Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
0.30.0
Overview
Revision 0.30.0 is a milestone revision for the TypeBox project. It is primarily focused on internal optimizations, refactoring work to reduce package and bundle sizes, enable increased modularity of internal sub modules (with some considerations given to future ESM publishing), renaming internal functions to address react native bundling issues and consolidating shared internal modules to reduce overall code overhead.
This revision also implements several new features, including new validation constraints for Array, new types for iterators, new utility types, a TypeScript code generation option for the compiler, enhancements made to modifiers and better options for TypeScript to TypeBox code translation. This revision also includes new examples including a transform type for handling IO encode an decode as well as a reference implementation for JSON Type Definition specification.
This revision includes breaking changes and some deprecations. It requires a minor semver revision.
Contents
TypeScript Code Generation
Revision 0.30.0 adds TypeScript code generation support to the TypeCompiler. By specifying the language option on the
.Code()
function, TypeBox will add type annotations to the compiled output. This functionality can be used to produce typed TS functions for projects that preference AOT compilation.Optional and Readonly
Revision 0.30.0 deprecates the
[Modifier]
symbol and introduces two new symbols,[Readonly]
and[Optional]
. This change is carried out to simplify type inference as well as to simplify runtime mapping logic. This change should not implicate users leveraging theType.*
purely for type composition, however implementors using TypeBox for reflection and code generation should update to the new symbols.Iterator and AsyncIterator
Revision 0.30.0 adds the types
Iterator
andAsyncIterator
. These types add to the existing non-validatable extended type set and can be used build callable generator functions. These types are written primarily to describe RPC network interfaces that return multiple values. Examples of which may include web socket streams or reading database result cursors over a network.Order Independent References
Revision 0.30.0 adds an overload for
Ref
to enable non order dependent type referencing. Prior to this revision, reference targets needed to be defined first before being referenced. Revision 0.30.0 lifts this restriction and allows referencing of "yet to be defined" targets through the use oftypeof
operator. This overload borrows on TypeScript's ability to derive type information irrespective of topological ordering.This overload is implemented for "TypeScript to TypeBox" code generation utilities where TypeScript types are not guaranteed ordered in a runtime sorted fashion.
Value Submodules
Revision 0.30.0 carries out a number of refactorings for the
Value.*
modules to enable each submodule to be imported individually. These refactorings are support better "pay to play" library characteristics, allowing users to import only the submodules they need. This update also makes provisions for ESM publishing by removing internal namespaces.The top level
Value.*
namespace will remain on all subsequent versions of TypeBox.Array Contains Constraint
Revision 0.30.0 implements validation support for the
contains
keyword as well as the draft 2019-09minContains
andmaxContains
constraints onArray
. Documentation on these constraints can be found https://json-schema.org/understanding-json-schema/reference/array.html#containsAdditional Utility Types
Revision 0.30.0 adds the utility types
Awaited
,Uppercase
,Lowercase
,Capitalize
, andUncapitalize
to the supported type set.A full list of TypeScript utility types can be found at this link.
Reduced Package Size
Revision 0.30.0 carries out several internal refactorings to reduce package and bundle sizes. This work is largely an ongoing process with provisional work carried out across
type
,value
andcompiler
modules. Revision 0.30.0 manages to weigh in slightly less than Revision 0.29.0 with the additional functionality provided on the revision.TypeBox Codegen
Revision 0.30.0 offers an external code generation API tool which can be used to programmatically convert TypeScript types into TypeBox types.
TypeBox-Code Project
JSON Type Definition
Revision 0.30.0 includes a reference implementation for JSON Type Definition (RFC 8927). This specification is currently under consideration for inclusion in the TypeBox library as an alternative schema representation for nominal type systems. The implementation currently contains all types expressed in the JSON Type Definition spec, but omits constraints such and
minimum
andmaximum
values (which are not formally represented in the specification).The implementation is offered as a single file which can be copied in to projects with TypeBox installed. This implementation may be enhanced over the next few revisions (with some potential to implement mapping types such as partial, required, omit, pick, keyof). This specification will be considered for inclusion under
@sinclair/typebox/typedef
if there is enough interest.Prototype Types
Revision 0.30.0 renames
Experimental
types toPrototype
types within the examples directory. Updates here include additional documentation and rationales for the existing typesUnionOneOf
,UnionEnum
,Const
, and includes two new typesEvaluate
andPartialDeep
. These types are written as standalone modules and can be copied into a project for direct use. The TypeBox project is open to community discussions around the inclusion of these types in future revisions.Transform Types
Revision 0.30.0 provides a reference implementation for Transform types. There has been some interest from users to offer combinators similar to Zod's
.transform()
function that permits remapping values during.parse()
like operations. As TypeBox types do not have fluent combinators or a parse function (and are just JSON Schema objects), introducing similar functionality without augmenting types or implementing a.parse()
on all types has proven to be particularily challenging.The reference Transform implementation implements a workable design by augmenting TypeBox types with codec functions outside the type system. These functions allow values to be structurally encoded and decoded through the
.parseLike()
functionsEncode()
andDecode()
. TypeBox adopts theio-ts
perspective for value transformation, viewing the act of transforming values primarily the role of dedicated codec system. As much of this functionality is considered high level and above and beyond the type system, Transform types will not likely be added to TypeBox type system; but rather added as an optional import in later revisions.Extended Type Representation Change
Revision 0.30.0 updates representations for all extended types. This change is made due to TypeBox's observed role as a general purpose JavaScript validation library as well as to deprecate support for extended type validation in Ajv which was only ever partially functional at best.
Attempts were made on Revision 0.25.0 to restructure extended types to provide Ajv hooks for custom type configuration. These hooks used the
type
property where{ type: 'object', instanceOf: 'Type' }
was used to configure schematics for JavaScript objects, and{ type: 'null', typeOf: 'Type' }
was used for JavaScript primitives. Despite these hooks, Ajv would still struggle with validation of primitive types (such asundefined
), and for the typesFunction
,Constructor
andPromise
; these were meaningless to Ajv and it did not make sense to try provide hooks for a validator that could not make use of them.This change represents a move towards a formal specification to express pure JavaScript constructs which is partially under discussion within the runtime type community. This change will implicate the use of
Uint8Array
andDate
objects when configuring for Ajv. A supplimentary fallback will be provided in the/examples
directory usingType.Unsafe
RegEx Renamed To RegExp
Revision 0.30.0 marks
Type.RegEx
as deprecated but providesType.RegExp
as an alternative (matching the JavaScriptRegExp
type name). Additionally this type has also been moved from theStandard
toExtended
type set. TheRegExp
type will no longer considered part of the Standard type set due to JavaScript Regular Expressions supporting a wider range of symbols and control characeters than is supported by the ECMA262 subset used by the JSON Schema specification. Information on the ECMA262 subset supported by JSON Schema can be found at the following Url https://json-schema.org/understanding-json-schema/reference/regular_expressions.htmlAs
Type.RegEx()
is widely used, this function will be retained under the@deprecated
annotation for the 0.30.0 revision.For Unicode (UTF-16) support on 0.30.0, the recommendation is to continue using user defined formats.
For information on configuring custom formats on Ajv, refer to https://ajv.js.org/guide/formats.html#user-defined-formats