@sinclair/typebox
Advanced tools
Comparing version 0.12.7 to 0.12.8
{ | ||
"name": "@sinclair/typebox", | ||
"version": "0.12.7", | ||
"version": "0.12.8", | ||
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript", | ||
@@ -33,5 +33,5 @@ "keywords": [ | ||
"smoke-task": "^1.1.2", | ||
"typescript": "^4.0.2", | ||
"typescript": "^4.1.2", | ||
"typescript-bundle": "^1.0.16" | ||
} | ||
} |
@@ -260,3 +260,3 @@ <div align='center'> | ||
TypeBox provides modifiers that can be applied to an objects properties. These allows for `optional` and `readonly` to be applied to that property. The following table illustates how they map between TypeScript and JSON Schema. | ||
TypeBox provides modifiers that can be applied to an objects properties. This allows for `optional` and `readonly` to be applied to that property. The following table illustates how they map between TypeScript and JSON Schema. | ||
@@ -394,3 +394,2 @@ ```typescript | ||
```typescript | ||
import { Type, Static } from '@sinclair/typebox' | ||
@@ -408,3 +407,3 @@ | ||
type ControllerInterface = Static<typeof ControllerInterface> | ||
const IController = Type.Object({ | ||
const ControllerInterface = Type.Object({ | ||
createRecord: Type.Function([CreateRecordRequest], Type.Promise(CreateRecordResponse)) | ||
@@ -478,2 +477,4 @@ }) | ||
const ajv = new Ajv() | ||
const User = Type.Object({ | ||
@@ -484,9 +485,46 @@ name: Type.String(), | ||
const ajv = new Ajv() | ||
const isValid = ajv.validate(User, { | ||
name: 'dave', | ||
email: 'dave@domain.com' | ||
}) | ||
const user = { name: 'dave', email: 'dave@domain.com' } | ||
// | ||
// isValid -> true | ||
``` | ||
const isValid = ajv.validate(User, user) | ||
// | ||
// -> true | ||
#### Strict | ||
By default, TypeBox will create `kind` and `modifier` properties on the underlying schemas. TypeBox uses these to help statically resolve the schemas to TypeScript types as well as apply the appropriate modifiers to an objects properties (such as optional). In most cases this is fine, however if using a validator that mandates on strict JSON schemas with known schema properties, you can use `Type.Strict()` to omit the `kind` and `modifier` properties. As follows. | ||
```typescript | ||
import { Type, Static } from '@sinclair/typebox' | ||
const T = Type.Object({ | ||
email: Type.Optional(Type.String()) | ||
}) | ||
// const T = { | ||
// kind: Symbol(ObjectKind), | ||
// type: 'object', | ||
// properties: { | ||
// email: { | ||
// kind: Symbol(StringKind), | ||
// type: 'string', | ||
// modifier: Symbol(OptionalModifier) | ||
// } | ||
// } | ||
// } | ||
const U = Type.Strict(Type.Object({ | ||
email: Type.Optional(Type.String()) | ||
})) | ||
// const U = { | ||
// type: 'object', | ||
// properties: { | ||
// email: { | ||
// type: 'string' | ||
// } | ||
// } | ||
// } | ||
``` |
@@ -256,3 +256,5 @@ export declare const ReadonlyOptionalModifier: unique symbol; | ||
Void(options?: CustomOptions): TVoid; | ||
/** `EXPERIMENTAL` Omits the `kind` and `modifier` properties from the given schema. */ | ||
Strict<T extends TSchema>(schema: T): T; | ||
} | ||
export declare const Type: TypeBuilder; |
@@ -115,4 +115,6 @@ "use strict"; | ||
const required_names = property_names.filter(name => !optional.includes(name)); | ||
const required = required_names.length ? required_names : undefined; | ||
return { ...options, kind: exports.ObjectKind, type: 'object', properties, required }; | ||
const required = (required_names.length > 0) ? required_names : undefined; | ||
return (required) ? | ||
{ ...options, kind: exports.ObjectKind, type: 'object', properties, required } : | ||
{ ...options, kind: exports.ObjectKind, type: 'object', properties }; | ||
} | ||
@@ -193,4 +195,8 @@ /** Creates a `{ [key: string]: T }` schema. */ | ||
} | ||
/** `EXPERIMENTAL` Omits the `kind` and `modifier` properties from the given schema. */ | ||
Strict(schema) { | ||
return JSON.parse(JSON.stringify(schema)); | ||
} | ||
} | ||
exports.TypeBuilder = TypeBuilder; | ||
exports.Type = new TypeBuilder(); |
59409
456
525