@sinclair/typebox
Advanced tools
Comparing version 0.16.1 to 0.16.2
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.16.1", | ||
"version": "0.16.2", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -181,2 +181,3 @@ <div align='center'> | ||
│ │ │ const: 42 │ | ||
│ │ │ type: 'number' │ | ||
│ │ │ } │ | ||
@@ -232,5 +233,5 @@ │ │ │ │ | ||
│ enum Foo { │ enum Foo { │ const T = { │ | ||
│ A, │ A, │ enum: [0, 1] │ | ||
│ B │ B │ } │ | ||
│ } │ } │ │ | ||
│ A, │ A, │ enum: [0, 1], │ | ||
│ B │ B │ type: 'number' │ | ||
│ } │ } │ } │ | ||
│ │ │ │ | ||
@@ -250,5 +251,5 @@ │ type T = Type.Enum(Foo) │ type T = Foo │ │ | ||
│ const T = Type.KeyOf( │ type T = keyof { │ const T = { │ | ||
│ Type.Object({ │ x: number, │ enum: ['x', 'y'] │ | ||
│ x: Type.Number(), │ y: number │ } │ | ||
│ y: Type.Number() │ } │ │ | ||
│ Type.Object({ │ x: number, │ enum: ['x', 'y'], │ | ||
│ x: Type.Number(), │ y: number │ type: 'string' │ | ||
│ y: Type.Number() │ } │ } │ | ||
│ }) │ │ │ | ||
@@ -255,0 +256,0 @@ │ ) │ │ │ |
@@ -88,4 +88,5 @@ export declare const ReadonlyOptionalModifier: unique symbol; | ||
kind: typeof KeyOfKind; | ||
type: 'string'; | ||
enum: [...T]; | ||
}; | ||
} & CustomOptions; | ||
export declare type TDict<T extends TSchema> = { | ||
@@ -107,2 +108,3 @@ kind: typeof DictKind; | ||
kind: typeof EnumKind; | ||
type?: 'string' | 'number' | ['string', 'number']; | ||
enum: T[]; | ||
@@ -109,0 +111,0 @@ } & CustomOptions; |
@@ -131,7 +131,14 @@ "use strict"; | ||
const values = Object.keys(item).filter(key => isNaN(key)).map(key => item[key]); | ||
return { ...options, kind: exports.EnumKind, enum: values }; | ||
if (values.length === 0) { | ||
return { ...options, kind: exports.EnumKind, enum: values }; | ||
} | ||
const type = typeof values[0]; | ||
if (values.some(value => typeof value !== type)) { | ||
return { ...options, kind: exports.EnumKind, type: ['string', 'number'], enum: values }; | ||
} | ||
return { ...options, kind: exports.EnumKind, type, enum: values }; | ||
} | ||
/** `STANDARD` Creates a literal schema. Supports `string | number | boolean` values. */ | ||
Literal(value, options = {}) { | ||
return { ...options, kind: exports.LiteralKind, const: value }; | ||
return { ...options, kind: exports.LiteralKind, const: value, type: typeof value }; | ||
} | ||
@@ -177,3 +184,3 @@ /** `STANDARD` Creates a `string` schema. */ | ||
const keys = Object.keys(schema.properties); | ||
return { ...options, kind: exports.KeyOfKind, enum: keys }; | ||
return { ...options, kind: exports.KeyOfKind, type: 'string', enum: keys }; | ||
} | ||
@@ -180,0 +187,0 @@ /** `STANDARD` Creates an intersection schema of the given object schemas. */ |
76275
561
614