fluent-json-schema
Advanced tools
Comparing version 4.0.0 to 4.1.0
{ | ||
"name": "fluent-json-schema", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "JSON Schema fluent API", | ||
@@ -48,7 +48,7 @@ "main": "src/FluentJSONSchema.js", | ||
"jest": "^29.0.1", | ||
"jsdoc-to-markdown": "^7.0.0", | ||
"jsdoc-to-markdown": "^8.0.0", | ||
"lodash.merge": "^4.6.2", | ||
"snazzy": "^9.0.0", | ||
"standard": "^17.0.0", | ||
"tsd": "^0.24.1" | ||
"tsd": "^0.25.0" | ||
}, | ||
@@ -55,0 +55,0 @@ "dependencies": { |
@@ -8,3 +8,3 @@ # fluent-json-schema | ||
[![Coverage Status](https://coveralls.io/repos/github/fastify/fluent-json-schema/badge.svg?branch=master)](https://coveralls.io/github/fastify/fluent-json-schema?branch=master) | ||
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat)](https://github.com/prettier/prettier) | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
@@ -135,2 +135,4 @@ ## Features | ||
### CommonJS | ||
With `"esModuleInterop": true` activated in the `tsconfig.json`: | ||
@@ -158,2 +160,15 @@ | ||
### ESM | ||
A named export is also available to work with native ESM modules: | ||
```typescript | ||
import { S } from 'fluent-json-schema' | ||
const schema = S.object() | ||
.prop('foo', S.string()) | ||
.prop('bar', S.number()) | ||
.valueOf() | ||
``` | ||
## Validation | ||
@@ -160,0 +175,0 @@ |
@@ -51,3 +51,3 @@ const { ArraySchema } = require('./ArraySchema') | ||
it('invalid', () => { | ||
expect(() => ArraySchema().items('')).toThrowError( | ||
expect(() => ArraySchema().items('')).toThrow( | ||
new S.FluentSchemaError("'items' must be a S or an array of S") | ||
@@ -84,3 +84,3 @@ ) | ||
it('invalid', () => { | ||
expect(() => ArraySchema().additionalItems('')).toThrowError( | ||
expect(() => ArraySchema().additionalItems('')).toThrow( | ||
new S.FluentSchemaError("'additionalItems' must be a boolean or a S") | ||
@@ -107,3 +107,3 @@ ) | ||
.valueOf() | ||
).toThrowError(new S.FluentSchemaError("'contains' must be a S")) | ||
).toThrow(new S.FluentSchemaError("'contains' must be a S")) | ||
}) | ||
@@ -128,3 +128,3 @@ }) | ||
.valueOf() | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError("'uniqueItems' must be a boolean") | ||
@@ -151,3 +151,3 @@ ) | ||
.valueOf() | ||
).toThrowError(new S.FluentSchemaError("'minItems' must be a integer")) | ||
).toThrow(new S.FluentSchemaError("'minItems' must be a integer")) | ||
}) | ||
@@ -172,3 +172,3 @@ }) | ||
.valueOf() | ||
).toThrowError(new S.FluentSchemaError("'maxItems' must be a integer")) | ||
).toThrow(new S.FluentSchemaError("'maxItems' must be a integer")) | ||
}) | ||
@@ -175,0 +175,0 @@ }) |
@@ -78,3 +78,3 @@ const { BaseSchema } = require('./BaseSchema') | ||
BaseSchema().id('') | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -126,3 +126,3 @@ 'id should not be an empty fragment <#> or an empty string <> (e.g. #myId)' | ||
.valueOf().examples | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -164,3 +164,3 @@ "'examples' must be an array e.g. ['1', 'one', 'foo']" | ||
.prop('A', S.string()).required().required() | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -176,3 +176,3 @@ "'required' has repeated keys, check your calls to .required()" | ||
.prop('A', S.string().required()) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -188,3 +188,3 @@ "'required' has repeated keys, check your calls to .required()" | ||
return S.object().required().valueOf() | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -226,3 +226,3 @@ "'required' has called on root-level schema, check your calls to .required()" | ||
.valueOf().deprecated | ||
).toEqual(true) | ||
).toBe(true) | ||
}) | ||
@@ -235,3 +235,3 @@ it('invalid', () => { | ||
.valueOf().deprecated | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -247,3 +247,3 @@ "'deprecated' must be a boolean value" | ||
.valueOf().deprecated | ||
).toEqual(true) | ||
).toBe(true) | ||
}) | ||
@@ -255,3 +255,3 @@ it('can be set to false', () => { | ||
.valueOf().deprecated | ||
).toEqual(false) | ||
).toBe(false) | ||
}) | ||
@@ -406,3 +406,3 @@ it('property', () => { | ||
.valueOf().examples | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -443,3 +443,3 @@ "'enums' must be an array with at least an element e.g. ['1', 'one', 'foo']" | ||
.valueOf().readOnly | ||
).toEqual(true) | ||
).toBe(true) | ||
}) | ||
@@ -451,3 +451,3 @@ it('valid with no value', () => { | ||
.valueOf().readOnly | ||
).toEqual(true) | ||
).toBe(true) | ||
}) | ||
@@ -459,3 +459,3 @@ it('can be set to false', () => { | ||
.valueOf().readOnly | ||
).toEqual(false) | ||
).toBe(false) | ||
}) | ||
@@ -470,3 +470,3 @@ }) | ||
.valueOf().writeOnly | ||
).toEqual(true) | ||
).toBe(true) | ||
}) | ||
@@ -478,3 +478,3 @@ it('valid with no value', () => { | ||
.valueOf().writeOnly | ||
).toEqual(true) | ||
).toBe(true) | ||
}) | ||
@@ -486,3 +486,3 @@ it('can be set to false', () => { | ||
.valueOf().writeOnly | ||
).toEqual(false) | ||
).toBe(false) | ||
}) | ||
@@ -530,3 +530,3 @@ }) | ||
return BaseSchema().allOf('test') | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -540,3 +540,3 @@ "'allOf' must be a an array of FluentSchema rather than a 'string'" | ||
return BaseSchema().allOf(['test']) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -591,3 +591,3 @@ "'allOf' must be a an array of FluentSchema rather than a 'object'" | ||
return BaseSchema().anyOf('test') | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -601,3 +601,3 @@ "'anyOf' must be a an array of FluentSchema rather than a 'string'" | ||
return BaseSchema().anyOf(['test']) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -625,3 +625,3 @@ "'anyOf' must be a an array of FluentSchema rather than a 'object'" | ||
return BaseSchema().oneOf('test') | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -635,3 +635,3 @@ "'oneOf' must be a an array of FluentSchema rather than a 'string'" | ||
return BaseSchema().oneOf(['test']) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -673,3 +673,3 @@ "'oneOf' must be a an array of FluentSchema rather than a 'object'" | ||
BaseSchema().not(undefined) | ||
}).toThrowError(new S.FluentSchemaError("'not' must be a BaseSchema")) | ||
}).toThrow(new S.FluentSchemaError("'not' must be a BaseSchema")) | ||
}) | ||
@@ -729,3 +729,3 @@ }) | ||
) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError("'ifClause' must be a BaseSchema") | ||
@@ -737,3 +737,3 @@ ) | ||
BaseSchema().ifThen(BaseSchema().id('id'), undefined) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError("'thenClause' must be a BaseSchema") | ||
@@ -802,3 +802,3 @@ ) | ||
) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError("'ifClause' must be a BaseSchema") | ||
@@ -814,3 +814,3 @@ ) | ||
) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError("'thenClause' must be a BaseSchema") | ||
@@ -826,3 +826,3 @@ ) | ||
) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -829,0 +829,0 @@ "'elseClause' must be a BaseSchema or a false boolean value" |
@@ -32,3 +32,3 @@ const { BooleanSchema } = require('./BooleanSchema') | ||
.valueOf().properties.prop.type | ||
).toEqual('boolean') | ||
).toBe('boolean') | ||
}) | ||
@@ -35,0 +35,0 @@ |
@@ -202,1 +202,2 @@ 'use strict' | ||
module.exports.default = fluentSchema | ||
module.exports.S = fluentSchema |
@@ -188,3 +188,3 @@ const basic = require('./schemas/basic') | ||
}) | ||
expect(validate.errors).toEqual(null) | ||
expect(validate.errors).toBeNull() | ||
expect(valid).toBeTruthy() | ||
@@ -221,3 +221,3 @@ }) | ||
const valid = validate({ foo: 'foo' }) | ||
expect(validate.errors).toEqual(null) | ||
expect(validate.errors).toBeNull() | ||
expect(valid).toBeTruthy() | ||
@@ -331,3 +331,3 @@ }) | ||
}) | ||
expect(validate.errors).toEqual(null) | ||
expect(validate.errors).toBeNull() | ||
expect(valid).toBeTruthy() | ||
@@ -501,3 +501,3 @@ }) | ||
}) | ||
expect(validate.errors).toEqual(null) | ||
expect(validate.errors).toBeNull() | ||
expect(valid).toBeTruthy() | ||
@@ -504,0 +504,0 @@ }) |
@@ -48,3 +48,3 @@ const { IntegerSchema } = require('./IntegerSchema') | ||
it('invalid number', () => { | ||
expect(() => S.integer().minimum('5.1')).toThrowError( | ||
expect(() => S.integer().minimum('5.1')).toThrow( | ||
new S.FluentSchemaError("'minimum' must be a Number") | ||
@@ -54,3 +54,3 @@ ) | ||
it('invalid integer', () => { | ||
expect(() => S.integer().minimum(5.1)).toThrowError( | ||
expect(() => S.integer().minimum(5.1)).toThrow( | ||
new S.FluentSchemaError("'minimum' must be an Integer") | ||
@@ -79,3 +79,3 @@ ) | ||
it('invalid number', () => { | ||
expect(() => S.integer().maximum('5.1')).toThrowError( | ||
expect(() => S.integer().maximum('5.1')).toThrow( | ||
new S.FluentSchemaError("'maximum' must be a Number") | ||
@@ -85,3 +85,3 @@ ) | ||
it('invalid float', () => { | ||
expect(() => S.integer().maximum(5.1)).toThrowError( | ||
expect(() => S.integer().maximum(5.1)).toThrow( | ||
new S.FluentSchemaError("'maximum' must be an Integer") | ||
@@ -111,3 +111,3 @@ ) | ||
it('invalid value', () => { | ||
expect(() => S.integer().multipleOf('5.1')).toThrowError( | ||
expect(() => S.integer().multipleOf('5.1')).toThrow( | ||
new S.FluentSchemaError("'multipleOf' must be a Number") | ||
@@ -117,3 +117,3 @@ ) | ||
it('invalid integer', () => { | ||
expect(() => S.integer().multipleOf(5.1)).toThrowError( | ||
expect(() => S.integer().multipleOf(5.1)).toThrow( | ||
new S.FluentSchemaError("'multipleOf' must be an Integer") | ||
@@ -144,3 +144,3 @@ ) | ||
it('invalid number', () => { | ||
expect(() => S.integer().exclusiveMinimum('5.1')).toThrowError( | ||
expect(() => S.integer().exclusiveMinimum('5.1')).toThrow( | ||
new S.FluentSchemaError("'exclusiveMinimum' must be a Number") | ||
@@ -150,3 +150,3 @@ ) | ||
it('invalid integer', () => { | ||
expect(() => S.integer().exclusiveMinimum(5.1)).toThrowError( | ||
expect(() => S.integer().exclusiveMinimum(5.1)).toThrow( | ||
new S.FluentSchemaError("'exclusiveMinimum' must be an Integer") | ||
@@ -176,3 +176,3 @@ ) | ||
it('invalid number', () => { | ||
expect(() => S.integer().exclusiveMaximum('5.1')).toThrowError( | ||
expect(() => S.integer().exclusiveMaximum('5.1')).toThrow( | ||
new S.FluentSchemaError("'exclusiveMaximum' must be a Number") | ||
@@ -182,3 +182,3 @@ ) | ||
it('invalid integer', () => { | ||
expect(() => S.integer().exclusiveMaximum(5.1)).toThrowError( | ||
expect(() => S.integer().exclusiveMaximum(5.1)).toThrow( | ||
new S.FluentSchemaError("'exclusiveMaximum' must be an Integer") | ||
@@ -185,0 +185,0 @@ ) |
@@ -54,3 +54,3 @@ const { MixedSchema } = require('./MixedSchema') | ||
S.mixed(types) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -66,3 +66,3 @@ "Invalid 'types'. It must be an array of types. Valid types are string | number | boolean | integer | object | array | null" | ||
S.mixed(types) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -69,0 +69,0 @@ "Invalid 'types'. It must be an array of types. Valid types are string | number | boolean | integer | object | array | null" |
@@ -32,3 +32,3 @@ const { NullSchema } = require('./NullSchema') | ||
.valueOf().properties.prop.type | ||
).toEqual('null') | ||
).toBe('null') | ||
}) | ||
@@ -35,0 +35,0 @@ |
@@ -48,3 +48,3 @@ const { NumberSchema } = require('./NumberSchema') | ||
it('invalid value', () => { | ||
expect(() => S.number().minimum('5.1')).toThrowError( | ||
expect(() => S.number().minimum('5.1')).toThrow( | ||
new S.FluentSchemaError("'minimum' must be a Number") | ||
@@ -73,3 +73,3 @@ ) | ||
it('invalid value', () => { | ||
expect(() => S.number().maximum('5.1')).toThrowError( | ||
expect(() => S.number().maximum('5.1')).toThrow( | ||
new S.FluentSchemaError("'maximum' must be a Number") | ||
@@ -99,3 +99,3 @@ ) | ||
it('invalid value', () => { | ||
expect(() => S.number().multipleOf('5.1')).toThrowError( | ||
expect(() => S.number().multipleOf('5.1')).toThrow( | ||
new S.FluentSchemaError("'multipleOf' must be a Number") | ||
@@ -126,3 +126,3 @@ ) | ||
it('invalid value', () => { | ||
expect(() => S.number().exclusiveMinimum('5.1')).toThrowError( | ||
expect(() => S.number().exclusiveMinimum('5.1')).toThrow( | ||
new S.FluentSchemaError("'exclusiveMinimum' must be a Number") | ||
@@ -152,3 +152,3 @@ ) | ||
it('invalid value', () => { | ||
expect(() => S.number().exclusiveMaximum('5.1')).toThrowError( | ||
expect(() => S.number().exclusiveMaximum('5.1')).toThrow( | ||
new S.FluentSchemaError("'exclusiveMaximum' must be a Number") | ||
@@ -155,0 +155,0 @@ ) |
@@ -99,3 +99,3 @@ const { ObjectSchema } = require('./ObjectSchema') | ||
ObjectSchema().id('') | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -211,3 +211,3 @@ 'id should not be an empty fragment <#> or an empty string <> (e.g. #myId)' | ||
.valueOf().properties.prop.type | ||
).toEqual('object') | ||
).toBe('object') | ||
}) | ||
@@ -308,3 +308,3 @@ | ||
ObjectSchema().prop('prop', 'invalid') | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -319,3 +319,3 @@ "'prop' doesn't support value '\"invalid\"'. Pass a FluentSchema object" | ||
ObjectSchema().prop('prop', 555) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -330,3 +330,3 @@ "'prop' doesn't support value '555'. Pass a FluentSchema object" | ||
ObjectSchema().prop('prop', []) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -378,3 +378,3 @@ "'prop' doesn't support value '[]'. Pass a FluentSchema object" | ||
).toEqual(value) | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -406,3 +406,3 @@ "'additionalProperties' must be a boolean or a S" | ||
).toEqual(value) | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError("'maxProperties' must be a Integer") | ||
@@ -432,3 +432,3 @@ ) | ||
).toEqual(value) | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError("'minProperties' must be a Integer") | ||
@@ -463,3 +463,3 @@ ) | ||
).toEqual(value) | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -521,3 +521,3 @@ "'patternProperties' invalid options. Provide a valid map e.g. { '^fo.*$': S.string() }" | ||
).toEqual(value) | ||
).toThrowError( | ||
).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -563,3 +563,3 @@ "'dependencies' invalid options. Provide a valid map e.g. { 'foo': ['bar'] } or { 'foo': S.string() }" | ||
).toEqual(value) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -605,3 +605,3 @@ "'dependentRequired' invalid options. Provide a valid array e.g. { 'foo': ['bar'] }" | ||
).toEqual(value) | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -635,3 +635,3 @@ "'dependentSchemas' invalid options. Provide a valid schema e.g. { 'foo': S.string() }" | ||
).toEqual(value) | ||
).toThrowError(new S.FluentSchemaError("'propertyNames' must be a S")) | ||
).toThrow(new S.FluentSchemaError("'propertyNames' must be a S")) | ||
}) | ||
@@ -643,3 +643,3 @@ }) | ||
it('sets a type object from the root', () => { | ||
expect(S.null().valueOf().type).toEqual('null') | ||
expect(S.null().valueOf().type).toBe('null') | ||
}) | ||
@@ -653,3 +653,3 @@ | ||
.valueOf().properties.value.type | ||
).toEqual('null') | ||
).toBe('null') | ||
}) | ||
@@ -899,3 +899,3 @@ }) | ||
S.object().extend() | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError("Schema can't be null or undefined") | ||
@@ -908,3 +908,3 @@ ) | ||
S.object().extend('boom!') | ||
}).toThrowError(new S.FluentSchemaError("Schema isn't FluentSchema type")) | ||
}).toThrow(new S.FluentSchemaError("Schema isn't FluentSchema type")) | ||
}) | ||
@@ -918,3 +918,3 @@ | ||
.prop('foo') | ||
}).toThrowError( | ||
}).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -921,0 +921,0 @@ 'S.object(...).extend(...).prop is not a function' |
@@ -36,3 +36,3 @@ const { RawSchema } = require('./RawSchema') | ||
it("throws an exception if the input isn't an object", () => { | ||
expect(() => RawSchema('boom!')).toThrowError( | ||
expect(() => RawSchema('boom!')).toThrow( | ||
new S.FluentSchemaError('A fragment must be a JSON object') | ||
@@ -39,0 +39,0 @@ ) |
@@ -46,3 +46,3 @@ const { StringSchema, FORMATS } = require('./StringSchema') | ||
it('invalid', () => { | ||
expect(() => StringSchema().minLength('5.1')).toThrowError( | ||
expect(() => StringSchema().minLength('5.1')).toThrow( | ||
new S.FluentSchemaError("'minLength' must be an Integer") | ||
@@ -63,3 +63,3 @@ ) | ||
it('invalid', () => { | ||
expect(() => StringSchema().maxLength('5.1')).toThrowError( | ||
expect(() => StringSchema().maxLength('5.1')).toThrow( | ||
new S.FluentSchemaError("'maxLength' must be an Integer") | ||
@@ -91,3 +91,3 @@ ) | ||
it('invalid', () => { | ||
expect(() => StringSchema().format('invalid')).toThrowError( | ||
expect(() => StringSchema().format('invalid')).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -133,3 +133,3 @@ "'format' must be one of relative-json-pointer, json-pointer, uuid, regex, ipv6, ipv4, hostname, email, url, uri-template, uri-reference, uri, time, date, date-time" | ||
it('invalid value', () => { | ||
expect(() => StringSchema().pattern(1111)).toThrowError( | ||
expect(() => StringSchema().pattern(1111)).toThrow( | ||
new S.FluentSchemaError( | ||
@@ -153,3 +153,3 @@ "'pattern' must be a string or a RegEx (e.g. /.*/)" | ||
it('invalid', () => { | ||
expect(() => StringSchema().contentEncoding(1000)).toThrowError( | ||
expect(() => StringSchema().contentEncoding(1000)).toThrow( | ||
new S.FluentSchemaError("'contentEncoding' must be a string") | ||
@@ -171,3 +171,3 @@ ) | ||
it('invalid', () => { | ||
expect(() => StringSchema().contentMediaType(1000)).toThrowError( | ||
expect(() => StringSchema().contentMediaType(1000)).toThrow( | ||
new S.FluentSchemaError("'contentMediaType' must be a string") | ||
@@ -174,0 +174,0 @@ ) |
@@ -117,16 +117,16 @@ export interface BaseSchema<T> { | ||
export interface ObjectSchema extends BaseSchema<ObjectSchema> { | ||
definition: (name: string, props?: JSONSchema) => ObjectSchema | ||
prop: (name: string, props?: JSONSchema) => ObjectSchema | ||
additionalProperties: (value: JSONSchema | boolean) => ObjectSchema | ||
maxProperties: (max: number) => ObjectSchema | ||
minProperties: (min: number) => ObjectSchema | ||
patternProperties: (options: PatternPropertiesOptions) => ObjectSchema | ||
dependencies: (options: DependenciesOptions) => ObjectSchema | ||
propertyNames: (value: JSONSchema) => ObjectSchema | ||
extend: (schema: ObjectSchema | ExtendedSchema) => ExtendedSchema | ||
only: (properties: string[]) => ObjectSchema | ||
without: (properties: string[]) => ObjectSchema | ||
dependentRequired: (options: DependentRequiredOptions) => ObjectSchema | ||
dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema | ||
export interface ObjectSchema<T extends Record<string, any> = Record<string, any>> extends BaseSchema<ObjectSchema<T>> { | ||
definition: (name: Key<T>, props?: JSONSchema) => ObjectSchema<T> | ||
prop: (name: Key<T>, props?: JSONSchema) => ObjectSchema<T> | ||
additionalProperties: (value: JSONSchema | boolean) => ObjectSchema<T> | ||
maxProperties: (max: number) => ObjectSchema<T> | ||
minProperties: (min: number) => ObjectSchema<T> | ||
patternProperties: (options: PatternPropertiesOptions) => ObjectSchema<T> | ||
dependencies: (options: DependenciesOptions) => ObjectSchema<T> | ||
propertyNames: (value: JSONSchema) => ObjectSchema<T> | ||
extend: (schema: ObjectSchema<T> | ExtendedSchema) => ExtendedSchema | ||
only: (properties: string[]) => ObjectSchema<T> | ||
without: (properties: string[]) => ObjectSchema<T> | ||
dependentRequired: (options: DependentRequiredOptions<T>) => ObjectSchema<T> | ||
dependentSchemas: (options: DependentSchemaOptions<T>) => ObjectSchema<T> | ||
} | ||
@@ -226,12 +226,12 @@ | ||
interface DependentSchemaOptions { | ||
[key: string]: JSONSchema | ||
} | ||
type Key<T> = keyof T | (string & {}) | ||
interface DependentRequiredOptions { | ||
[key: string]: string[] | ||
} | ||
type DependentSchemaOptions<T extends Partial<Record<string, JSONSchema>>> = Partial<Record<keyof T, JSONSchema>> | ||
type DependentRequiredOptions<T extends Partial<Record<string, string[]>>> = Partial<Record<keyof T, string[]>> | ||
export function withOptions<T>(options: SchemaOptions): T | ||
type ObjectPlaceholder = Record<string | number | symbol, any>; | ||
export interface S extends BaseSchema<S> { | ||
@@ -243,3 +243,3 @@ string: () => StringSchema | ||
array: () => ArraySchema | ||
object: () => ObjectSchema | ||
object: <T extends ObjectPlaceholder = ObjectPlaceholder>() => ObjectSchema<T> | ||
null: () => NullSchema | ||
@@ -262,3 +262,4 @@ mixed: < | ||
declare var s: S | ||
export default s | ||
export declare var S: S | ||
export default S |
@@ -127,3 +127,3 @@ // This file will be passed to the TypeScript CLI to verify our typings compile | ||
}) | ||
.prop('foo', S.object().prop('bar')) | ||
.prop('bar', S.object().prop('bar')) | ||
.valueOf() | ||
@@ -139,1 +139,54 @@ | ||
console.log('deprecatedSchema:\n', JSON.stringify(deprecatedSchema)) | ||
type Foo = { | ||
foo: string | ||
bar: string | ||
} | ||
const dependentRequiredWithType = S.object<Foo>() | ||
.dependentRequired({ | ||
foo: ['bar'], | ||
}) | ||
.prop('foo') | ||
.prop('bar') | ||
.valueOf() | ||
console.log('dependentRequired:\n', JSON.stringify(dependentRequiredWithType)) | ||
const dependentSchemasWithType = S.object<Foo>() | ||
.dependentSchemas({ | ||
foo: S.object().prop('bar'), | ||
}) | ||
.prop('bar', S.object().prop('bar')) | ||
.valueOf() | ||
console.log('dependentSchemasWithType:\n', JSON.stringify(dependentSchemasWithType)) | ||
const deprecatedSchemaWithType = S.object<Foo>() | ||
.deprecated() | ||
.prop('foo', S.string().deprecated()) | ||
.valueOf() | ||
console.log('deprecatedSchemaWithType:\n', JSON.stringify(deprecatedSchemaWithType)) | ||
type ReallyLongType = { | ||
foo: string | ||
bar: string | ||
baz: string | ||
xpto: string | ||
abcd: number | ||
kct: { | ||
a: string | ||
b: number | ||
d: null | ||
} | ||
} | ||
const deepTestOnTypes = S.object<ReallyLongType>() | ||
.prop('bar', S.object().prop('bar')) | ||
// you can provide any string, to avoid breaking changes | ||
.prop('aaaa', S.anyOf([S.string()])) | ||
.definition('abcd', S.number()) | ||
.valueOf() | ||
console.log('deepTestOnTypes:\n', JSON.stringify(deepTestOnTypes)) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
267238
6534
374