@sinclair/typebox 0.31.13 → 0.31.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.31.13",
3
+ "version": "0.31.15",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -58,11 +58,9 @@ type T = Static<typeof T> // type T = {
58
58
 
59
59
  ## Overview
60
60
 
61
- TypeBox is a runtime type builder that creates Json Schema objects that infer as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript or runtime asserted using standard Json Schema validation.
61
+ TypeBox is a runtime type builder that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type assertion rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
62
62
 
63
- TypeBox uses industry standard schematics for runtime type representation; enabling types to be reflected, serialized and published directly. Its type system is fully extensible and able to support type representation for multiple schema specifications. It also provides a high performance validation compiler, various tools for working with dynamic data and offers detailed structured error reporting.
64
-
65
- TypeBox can be used as a simple tool to build up complex schemas or integrated into applications and frameworks to enable high performance runtime type checking for data received over the wire.
63
+ This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
66
64
 
67
65
  License MIT
68
66
 
package/typebox.d.ts CHANGED
@@ -72,7 +72,7 @@ export interface TSchema extends SchemaOptions, TKind {
72
72
  params: unknown[];
73
73
  static: unknown;
74
74
  }
75
- export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate | TFunction | TInteger | TIntersect | TIterator | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
75
+ export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TIntersect | TIterator | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
76
76
  export interface NumericOptions<N extends number | bigint> extends SchemaOptions {
77
77
  exclusiveMaximum?: N;
78
78
  exclusiveMinimum?: N;
@@ -160,10 +160,12 @@ export interface TDate extends TSchema, DateOptions {
160
160
  export type TEnumRecord = Record<TEnumKey, TEnumValue>;
161
161
  export type TEnumValue = string | number;
162
162
  export type TEnumKey = string;
163
- export type TEnumToLiteralUnion<T extends TEnumValue> = T extends TEnumValue ? (string extends T ? TNever : TLiteral<T>) : never;
164
- export type TEnumToLiteralTuple<T extends TEnumValue> = UnionToTuple<TEnumToLiteralUnion<T>>;
165
- export type TEnumToUnion<T extends TEnumValue> = UnionType<AssertRest<TEnumToLiteralTuple<T>>>;
166
- export type TEnum<T extends TEnumRecord> = TEnumToUnion<T[keyof T]>;
163
+ export interface TEnum<T extends Record<string, string | number> = Record<string, string | number>> extends TSchema {
164
+ [Kind]: 'Union';
165
+ [Hint]: 'Enum';
166
+ static: T[keyof T];
167
+ anyOf: TLiteral<T[keyof T]>[];
168
+ }
167
169
  export type TExtends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema> = (Static<L> extends Static<R> ? T : U) extends infer O ? UnionToTuple<O> extends [infer X, infer Y] ? TUnion<[AssertType<X>, AssertType<Y>]> : AssertType<O> : never;
168
170
  export type TExcludeTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{
169
171
  [K in T]: TLiteral<K>;
@@ -438,7 +440,7 @@ export type DecodeProperties<T extends TProperties> = {
438
440
  [K in keyof T]: DecodeType<T[K]>;
439
441
  };
440
442
  export type DecodeRest<T extends TSchema[]> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? [DecodeType<L>, ...DecodeRest<R>] : [];
441
- export type DecodeType<T extends TSchema> = (T extends TOptional<infer S extends TSchema> ? TOptional<DecodeType<S>> : T extends TReadonly<infer S extends TSchema> ? TReadonly<DecodeType<S>> : T extends TTransform<infer _, infer R> ? TUnsafe<R> : T extends TArray<infer S extends TSchema> ? TArray<DecodeType<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DecodeType<S>> : T extends TConstructor<infer P extends TSchema[], infer R extends TSchema> ? TConstructor<P, DecodeType<R>> : T extends TFunction<infer P extends TSchema[], infer R extends TSchema> ? TFunction<P, DecodeType<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<DecodeRest<S>> : T extends TIterator<infer S extends TSchema> ? TIterator<DecodeType<S>> : T extends TNot<infer S extends TSchema> ? TNot<DecodeType<S>> : T extends TObject<infer S> ? TObject<Evaluate<DecodeProperties<S>>> : T extends TPromise<infer S extends TSchema> ? TPromise<DecodeType<S>> : T extends TRecord<infer K, infer S> ? TRecord<K, DecodeType<S>> : T extends TRecursive<infer S extends TSchema> ? TRecursive<DecodeType<S>> : T extends TRef<infer S extends TSchema> ? TRef<DecodeType<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<DecodeRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<DecodeRest<S>> : T);
443
+ export type DecodeType<T extends TSchema> = (T extends TOptional<infer S extends TSchema> ? TOptional<DecodeType<S>> : T extends TReadonly<infer S extends TSchema> ? TReadonly<DecodeType<S>> : T extends TTransform<infer _, infer R> ? TUnsafe<R> : T extends TArray<infer S extends TSchema> ? TArray<DecodeType<S>> : T extends TAsyncIterator<infer S extends TSchema> ? TAsyncIterator<DecodeType<S>> : T extends TConstructor<infer P extends TSchema[], infer R extends TSchema> ? TConstructor<P, DecodeType<R>> : T extends TEnum<infer S> ? TEnum<S> : T extends TFunction<infer P extends TSchema[], infer R extends TSchema> ? TFunction<P, DecodeType<R>> : T extends TIntersect<infer S extends TSchema[]> ? TIntersect<DecodeRest<S>> : T extends TIterator<infer S extends TSchema> ? TIterator<DecodeType<S>> : T extends TNot<infer S extends TSchema> ? TNot<DecodeType<S>> : T extends TObject<infer S> ? TObject<Evaluate<DecodeProperties<S>>> : T extends TPromise<infer S extends TSchema> ? TPromise<DecodeType<S>> : T extends TRecord<infer K, infer S> ? TRecord<K, DecodeType<S>> : T extends TRecursive<infer S extends TSchema> ? TRecursive<DecodeType<S>> : T extends TRef<infer S extends TSchema> ? TRef<DecodeType<S>> : T extends TTuple<infer S extends TSchema[]> ? TTuple<DecodeRest<S>> : T extends TUnion<infer S extends TSchema[]> ? TUnion<DecodeRest<S>> : T);
442
444
  export type TransformFunction<T = any, U = any> = (value: T) => U;
443
445
  export interface TransformOptions<I extends TSchema = TSchema, O extends unknown = unknown> {
444
446
  Decode: TransformFunction<StaticDecode<I>, O>;
package/typebox.js CHANGED
@@ -1934,13 +1934,11 @@ class JsonTypeBuilder extends TypeBuilder {
1934
1934
  }
1935
1935
  /** `[Json]` Creates a Enum type */
1936
1936
  Enum(item, options = {}) {
1937
- if (ValueGuard.IsUndefined(item))
1938
- return this.Never(options);
1939
1937
  // prettier-ignore
1940
1938
  const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
1941
1939
  const values2 = [...new Set(values1)];
1942
1940
  const anyOf = values2.map((value) => exports.Type.Literal(value));
1943
- return this.Union(anyOf, options);
1941
+ return this.Union(anyOf, { ...options, [exports.Hint]: 'Enum' });
1944
1942
  }
1945
1943
  /** `[Json]` Creates a Conditional type */
1946
1944
  Extends(left, right, trueType, falseType, options = {}) {
package/value/hash.js CHANGED
@@ -66,6 +66,15 @@ const F64 = new Float64Array(1);
66
66
  const F64In = new DataView(F64.buffer);
67
67
  const F64Out = new Uint8Array(F64.buffer);
68
68
  // --------------------------------------------------------------------------
69
+ // NumberToBytes
70
+ // --------------------------------------------------------------------------
71
+ function* NumberToBytes(value) {
72
+ const byteCount = value === 0 ? 1 : Math.ceil(Math.floor(Math.log2(value) + 1) / 8);
73
+ for (let i = 0; i < byteCount; i++) {
74
+ yield (value >> (8 * (byteCount - 1 - i))) & 0xff;
75
+ }
76
+ }
77
+ // --------------------------------------------------------------------------
69
78
  // Hashing Functions
70
79
  // --------------------------------------------------------------------------
71
80
  function ArrayType(value) {
@@ -109,7 +118,9 @@ function ObjectType(value) {
109
118
  function StringType(value) {
110
119
  FNV1A64(ByteMarker.String);
111
120
  for (let i = 0; i < value.length; i++) {
112
- FNV1A64(value.charCodeAt(i));
121
+ for (const byte of NumberToBytes(value.charCodeAt(i))) {
122
+ FNV1A64(byte);
123
+ }
113
124
  }
114
125
  }
115
126
  function SymbolType(value) {