Skip to content

Version 1.1.39#1603

Merged
sinclairzx81 merged 3 commits into
mainfrom
with
May 28, 2026
Merged

Version 1.1.39#1603
sinclairzx81 merged 3 commits into
mainfrom
with

Conversation

@sinclairzx81
Copy link
Copy Markdown
Owner

@sinclairzx81 sinclairzx81 commented May 28, 2026

This PR adds an experimental syntax for annotating validation constraints and metadata on embedded Script types. It is written as a means to avoid Options<Type, Json> wrapping .. as well as to express broader TypeBox library patterns related to option passing (i.e. the last argument rule)

The with keyword

The with keyword is a syntactic shorthand for Options<Type, Json> that enables options and constraints to be naturally inlined to their associated type. It also serves to more clearly communicate that the right-hand side of Options<Type, Json> is a parsed Json value (and not a type expression)

// Type
const Email = Type.String({ format: 'email' })

// Script
const Email = Type.Script(`string with { format: 'email' }`)

The following 3 mechanisms are available for Script annotation assignment.

// ----------------------------------------------------------------------------
// Options 
// ----------------------------------------------------------------------------
const VectorA = Type.Options(Type.Object({
  x: Type.Options(Type.Number(), { minimum: 0, maximum: 1 }),
  y: Type.Options(Type.Number(), { minimum: 0, maximum: 1 })
}), { additionalProperties: false })

// ----------------------------------------------------------------------------
// Options + Script (Portable TS <-> TB)
// ----------------------------------------------------------------------------
const VectorB = Type.Script(`Options<{
  x: Options<number, { minimum: 0, maximum: 1 }>,
  y: Options<number, { minimum: 0, maximum: 1 }>,
}, { additionalProperties: false }>`)

// ----------------------------------------------------------------------------
// With Keyword (Experimental, TB only)
// ----------------------------------------------------------------------------
const VectorC = Type.Script(`{
  x: number with { minimum: 0, maximum: 1 }
  y: number with { minimum: 0, maximum: 1 }
} with { additionalProperties: false }`)

The with annotation syntax will be trialed over the 1.1.x revision.

@sinclairzx81 sinclairzx81 merged commit 1d1006e into main May 28, 2026
6 checks passed
@sinclairzx81 sinclairzx81 deleted the with branch May 28, 2026 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant