fluent-json-schema
Advanced tools
Comparing version
635
docs/API.md
@@ -127,2 +127,4 @@ ## Functions | ||
<li>Examples:</li> | ||
</ul> | ||
<ul> | ||
<li>S.number().raw({ nullable:true })</li> | ||
@@ -132,3 +134,3 @@ <li>S.string().format('date').raw({ formatMaximum: '2020-01-01' })</li> | ||
</dd> | ||
<dt><a href="#valueOf">valueOf()</a> ⇒ <code><a href="#object">object</a></code></dt> | ||
<dt><a href="#valueOf">valueOf([options])</a> ⇒ <code><a href="#object">object</a></code></dt> | ||
<dd><p>It returns all the schema values</p> | ||
@@ -178,2 +180,4 @@ </dd> | ||
<li>Examples:</li> | ||
</ul> | ||
<ul> | ||
<li>S.raw({ nullable:true, format: 'date', formatMaximum: '2020-01-01' })</li> | ||
@@ -253,2 +257,12 @@ <li>S.string().format('date').raw({ formatMaximum: '2020-01-01' })</li> | ||
</dd> | ||
<dt><a href="#dependentRequired">dependentRequired(opts)</a> ⇒ <code>FluentSchema</code></dt> | ||
<dd><p>The value of "properties" MUST be an object. Each dependency value MUST be an array. | ||
Each element in the array MUST be a string and MUST be unique. If the dependency key is a property in the instance, each of the items in the dependency value must be a property that exists in the instance.</p> | ||
<p><a href="https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.5.4">reference</a></p> | ||
</dd> | ||
<dt><a href="#dependentSchemas">dependentSchemas(opts)</a> ⇒ <code>FluentSchema</code></dt> | ||
<dd><p>The value of "properties" MUST be an object. The dependency value MUST be a valid JSON Schema. | ||
Each dependency key is a property in the instance and the entire instance must validate against the dependency value.</p> | ||
<p><a href="https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.2.4">reference</a></p> | ||
</dd> | ||
<dt><a href="#propertyNames">propertyNames(value)</a> ⇒ <code>FluentSchema</code></dt> | ||
@@ -263,2 +277,5 @@ <dd><p>If the instance is an object, this keyword validates if every property name in the instance validates against the provided schema. | ||
</dd> | ||
<dt><a href="#only">only(properties)</a> ⇒ <code><a href="#ObjectSchema">ObjectSchema</a></code></dt> | ||
<dd><p>Returns an object schema with only a subset of keys provided</p> | ||
</dd> | ||
<dt><a href="#definition">definition(name, props)</a> ⇒ <code>FluentSchema</code></dt> | ||
@@ -310,12 +327,11 @@ <dd><p>The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema. | ||
## ArraySchema([options]) ⇒ [<code>ArraySchema</code>](#ArraySchema) | ||
Represents a ArraySchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ------------------------------------------ | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -325,3 +341,2 @@ <a name="items"></a> | ||
## items(items) ⇒ <code>FluentSchema</code> | ||
This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself. | ||
@@ -334,7 +349,7 @@ If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | -------------------------------------------------------------------- | | ||
| items | <code>FluentSchema</code> \| <code>Array.<FluentSchema></code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| items | <code>FluentSchema</code> \| <code>Array.<FluentSchema></code> | | ||
@@ -344,3 +359,2 @@ <a name="additionalItems"></a> | ||
## additionalItems(items) ⇒ <code>FluentSchema</code> | ||
This keyword determines how child instances validate for arrays, and does not directly validate the immediate instance itself. | ||
@@ -350,7 +364,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------------------------------------- | | ||
| items | <code>FluentSchema</code> \| [<code>boolean</code>](#boolean) | | ||
| Param | Type | | ||
| --- | --- | | ||
| items | <code>FluentSchema</code> \| [<code>boolean</code>](#boolean) | | ||
@@ -360,3 +374,2 @@ <a name="contains"></a> | ||
## contains(value) ⇒ <code>FluentSchema</code> | ||
An array instance is valid against "contains" if at least one of its elements is valid against the given schema. | ||
@@ -366,7 +379,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------- | | ||
| value | <code>FluentSchema</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| value | <code>FluentSchema</code> | | ||
@@ -376,3 +389,2 @@ <a name="uniqueItems"></a> | ||
## uniqueItems(boolean) ⇒ <code>FluentSchema</code> | ||
If this keyword has boolean value false, the instance validates successfully. | ||
@@ -384,7 +396,7 @@ If it has boolean value true, the instance validates successfully if all of its elements are unique. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------- | -------------------------------- | | ||
| boolean | [<code>boolean</code>](#boolean) | | ||
| Param | Type | | ||
| --- | --- | | ||
| boolean | [<code>boolean</code>](#boolean) | | ||
@@ -394,3 +406,2 @@ <a name="minItems"></a> | ||
## minItems(min) ⇒ <code>FluentSchema</code> | ||
An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword. | ||
@@ -401,7 +412,7 @@ Omitting this keyword has the same behavior as a value of 0. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| min | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| min | [<code>number</code>](#number) | | ||
@@ -411,3 +422,2 @@ <a name="maxItems"></a> | ||
## maxItems(max) ⇒ <code>FluentSchema</code> | ||
An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword. | ||
@@ -418,7 +428,7 @@ Omitting this keyword has the same behavior as a value of 0. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| max | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| max | [<code>number</code>](#number) | | ||
@@ -428,12 +438,11 @@ <a name="BaseSchema"></a> | ||
## BaseSchema([options]) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
Represents a BaseSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | -------------------------------------- | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>BaseSchema</code>](#BaseSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>BaseSchema</code>](#BaseSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -443,3 +452,2 @@ <a name="id"></a> | ||
## id(id) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
It defines a URI for the schema, and the base URI that other URI references within the schema are resolved against. | ||
@@ -449,7 +457,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Description | | ||
| ----- | ------------------------------ | ----------- | | ||
| id | [<code>string</code>](#string) | an #id | | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| id | [<code>string</code>](#string) | an #id | | ||
@@ -459,3 +467,2 @@ <a name="title"></a> | ||
## title(title) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
It can be used to decorate a user interface with information about the data produced by this user interface. A title will preferably be short. | ||
@@ -465,7 +472,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| title | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| title | [<code>string</code>](#string) | | ||
@@ -475,3 +482,2 @@ <a name="description"></a> | ||
## description(description) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
It can be used to decorate a user interface with information about the data | ||
@@ -483,7 +489,7 @@ produced by this user interface. A description provides explanation about | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----------- | ------------------------------ | | ||
| description | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| description | [<code>string</code>](#string) | | ||
@@ -493,3 +499,2 @@ <a name="examples"></a> | ||
## examples(examples) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
The value of this keyword MUST be an array. | ||
@@ -500,7 +505,7 @@ There are no restrictions placed on the values within the array. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| -------- | ------------------------------ | | ||
| examples | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| examples | [<code>string</code>](#string) | | ||
@@ -510,10 +515,9 @@ <a name="ref"></a> | ||
## ref(ref) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
The value must be a valid id e.g. #properties/foo | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| ref | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| ref | [<code>string</code>](#string) | | ||
@@ -523,3 +527,2 @@ <a name="enum"></a> | ||
## enum(values) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
The value of this keyword MUST be an array. This array SHOULD have at least one element. Elements in the array SHOULD be unique. | ||
@@ -529,7 +532,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------ | ---------------------------- | | ||
| values | [<code>array</code>](#array) | | ||
| Param | Type | | ||
| --- | --- | | ||
| values | [<code>array</code>](#array) | | ||
@@ -539,3 +542,2 @@ <a name="const"></a> | ||
## const(value) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
The value of this keyword MAY be of any type, including null. | ||
@@ -545,7 +547,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | | ||
| ----- | | ||
| value | | ||
| --- | | ||
| value | | ||
@@ -555,3 +557,2 @@ <a name="default"></a> | ||
## default(defaults) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
There are no restrictions placed on the value of this keyword. | ||
@@ -561,7 +562,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | | ||
| -------- | | ||
| defaults | | ||
| Param | | ||
| --- | | ||
| defaults | | ||
@@ -571,3 +572,2 @@ <a name="readOnly"></a> | ||
## readOnly(isReadOnly) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
The value of readOnly can be left empty to indicate the property is readOnly. | ||
@@ -578,7 +578,7 @@ It takes an optional boolean which can be used to explicitly set readOnly true/false. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ---------- | ---------------------------------------------------------- | | ||
| isReadOnly | [<code>boolean</code>](#boolean) \| <code>undefined</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| isReadOnly | [<code>boolean</code>](#boolean) \| <code>undefined</code> | | ||
@@ -588,3 +588,2 @@ <a name="writeOnly"></a> | ||
## writeOnly(isWriteOnly) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
The value of writeOnly can be left empty to indicate the property is writeOnly. | ||
@@ -595,7 +594,7 @@ It takes an optional boolean which can be used to explicitly set writeOnly true/false. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----------- | ---------------------------------------------------------- | | ||
| isWriteOnly | [<code>boolean</code>](#boolean) \| <code>undefined</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| isWriteOnly | [<code>boolean</code>](#boolean) \| <code>undefined</code> | | ||
@@ -605,6 +604,4 @@ <a name="required"></a> | ||
## required() ⇒ <code>FluentSchema</code> | ||
Required has to be chained to a property: | ||
Examples: | ||
- S.prop('prop').required() | ||
@@ -620,3 +617,2 @@ - S.prop('prop', S.number()).required() | ||
## not(not) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
This keyword's value MUST be a valid JSON Schema. | ||
@@ -627,7 +623,7 @@ An instance is valid against this keyword if it fails to validate successfully against the schema defined by this keyword. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------- | | ||
| not | <code>FluentSchema</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| not | <code>FluentSchema</code> | | ||
@@ -637,3 +633,2 @@ <a name="anyOf"></a> | ||
## anyOf(schemas) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
It MUST be a non-empty array. Each item of the array MUST be a valid JSON Schema. | ||
@@ -643,7 +638,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------- | ---------------------------- | | ||
| schemas | [<code>array</code>](#array) | | ||
| Param | Type | | ||
| --- | --- | | ||
| schemas | [<code>array</code>](#array) | | ||
@@ -653,3 +648,2 @@ <a name="allOf"></a> | ||
## allOf(schemas) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
It MUST be a non-empty array. Each item of the array MUST be a valid JSON Schema. | ||
@@ -659,7 +653,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------- | ---------------------------- | | ||
| schemas | [<code>array</code>](#array) | | ||
| Param | Type | | ||
| --- | --- | | ||
| schemas | [<code>array</code>](#array) | | ||
@@ -669,3 +663,2 @@ <a name="oneOf"></a> | ||
## oneOf(schemas) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
It MUST be a non-empty array. Each item of the array MUST be a valid JSON Schema. | ||
@@ -675,7 +668,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------- | ---------------------------- | | ||
| schemas | [<code>array</code>](#array) | | ||
| Param | Type | | ||
| --- | --- | | ||
| schemas | [<code>array</code>](#array) | | ||
@@ -685,3 +678,2 @@ <a name="ifThen"></a> | ||
## ifThen(ifClause, thenClause) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
This validation outcome of this keyword's subschema has no direct effect on the overall validation result. | ||
@@ -692,7 +684,7 @@ Rather, it controls which of the "then" or "else" keywords are evaluated. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Description | | ||
| ---------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------ | | ||
| ifClause | [<code>BaseSchema</code>](#BaseSchema) | [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.6.1) | | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| ifClause | [<code>BaseSchema</code>](#BaseSchema) | [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.6.1) | | ||
| thenClause | [<code>BaseSchema</code>](#BaseSchema) | [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.6.2) | | ||
@@ -703,11 +695,10 @@ | ||
## ifThenElse(ifClause, thenClause, elseClause) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
When "if" is present, and the instance fails to validate against its subschema, | ||
then validation succeeds against this keyword if the instance successfully validates against this keyword's subschema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Description | | ||
| ---------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------ | | ||
| ifClause | [<code>BaseSchema</code>](#BaseSchema) | [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.6.1) | | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| ifClause | [<code>BaseSchema</code>](#BaseSchema) | [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.6.1) | | ||
| thenClause | [<code>BaseSchema</code>](#BaseSchema) | [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.6.2) | | ||
@@ -719,15 +710,13 @@ | elseClause | [<code>BaseSchema</code>](#BaseSchema) | [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.6.3) | | ||
## raw(fragment) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
Because the differences between JSON Schemas and Open API (Swagger) | ||
it can be handy to arbitrary modify the schema injecting a fragment | ||
- Examples: | ||
* Examples: | ||
- S.number().raw({ nullable:true }) | ||
- S.string().format('date').raw({ formatMaximum: '2020-01-01' }) | ||
* S.number().raw({ nullable:true }) | ||
* S.string().format('date').raw({ formatMaximum: '2020-01-01' }) | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Description | | ||
| -------- | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- | | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fragment | [<code>string</code>](#string) | an arbitrary JSON Schema to inject [reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.3.3) | | ||
@@ -737,20 +726,24 @@ | ||
## valueOf() ⇒ [<code>object</code>](#object) | ||
## valueOf([options]) ⇒ [<code>object</code>](#object) | ||
It returns all the schema values | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.isRoot] | [<code>boolean</code>](#boolean) | <code>true</code> | Is a root level schema | | ||
<a name="BooleanSchema"></a> | ||
## BooleanSchema([options]) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
Represents a BooleanSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ------------------------------------------ | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -760,11 +753,10 @@ <a name="S"></a> | ||
## S([options]) ⇒ [<code>S</code>](#S) | ||
Represents a S. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | -------------------------------- | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>S</code>](#S) | | Default schema | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>S</code>](#S) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -775,3 +767,2 @@ | ||
## string() ⇒ [<code>StringSchema</code>](#StringSchema) | ||
Set a property to type string | ||
@@ -785,3 +776,2 @@ | ||
## number() ⇒ [<code>NumberSchema</code>](#NumberSchema) | ||
Set a property to type number | ||
@@ -795,3 +785,2 @@ | ||
## integer() ⇒ [<code>IntegerSchema</code>](#IntegerSchema) | ||
Set a property to type integer | ||
@@ -805,3 +794,2 @@ | ||
## boolean() ⇒ [<code>BooleanSchema</code>](#BooleanSchema) | ||
Set a property to type boolean | ||
@@ -815,3 +803,2 @@ | ||
## array() ⇒ [<code>ArraySchema</code>](#ArraySchema) | ||
Set a property to type array | ||
@@ -825,3 +812,2 @@ | ||
## object() ⇒ [<code>ObjectSchema</code>](#ObjectSchema) | ||
Set a property to type object | ||
@@ -835,3 +821,2 @@ | ||
## null() ⇒ [<code>NullSchema</code>](#NullSchema) | ||
Set a property to type null | ||
@@ -845,10 +830,9 @@ | ||
## mixed(types) ⇒ [<code>MixedSchema</code>](#MixedSchema) | ||
A mixed schema is the union of multiple types (e.g. ['string', 'integer'] | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------------------------------------ | | ||
| types | [<code>[ 'Array' ].<string></code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| types | [<code>Array.<string></code>](#string) | | ||
@@ -858,15 +842,13 @@ <a name="raw"></a> | ||
## raw(fragment) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
Because the differences between JSON Schemas and Open API (Swagger) | ||
it can be handy to arbitrary modify the schema injecting a fragment | ||
- Examples: | ||
* Examples: | ||
- S.raw({ nullable:true, format: 'date', formatMaximum: '2020-01-01' }) | ||
- S.string().format('date').raw({ formatMaximum: '2020-01-01' }) | ||
* S.raw({ nullable:true, format: 'date', formatMaximum: '2020-01-01' }) | ||
* S.string().format('date').raw({ formatMaximum: '2020-01-01' }) | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Description | | ||
| -------- | ------------------------------ | ---------------------------------- | | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fragment | [<code>string</code>](#string) | an arbitrary JSON Schema to inject | | ||
@@ -877,12 +859,11 @@ | ||
## IntegerSchema([options]) ⇒ [<code>NumberSchema</code>](#NumberSchema) | ||
Represents a NumberSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ------------------------------------------ | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>NumberSchema</code>](#NumberSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>NumberSchema</code>](#NumberSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -892,12 +873,11 @@ <a name="MixedSchema"></a> | ||
## MixedSchema([options]) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
Represents a MixedSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ---------------------------------------- | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>MixedSchema</code>](#MixedSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>MixedSchema</code>](#MixedSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -907,12 +887,11 @@ <a name="NullSchema"></a> | ||
## NullSchema([options]) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
Represents a NullSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ------------------------------------------ | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -922,3 +901,2 @@ <a name="null"></a> | ||
## null() ⇒ <code>FluentSchema</code> | ||
Set a property to type null | ||
@@ -932,12 +910,11 @@ | ||
## NumberSchema([options]) ⇒ [<code>NumberSchema</code>](#NumberSchema) | ||
Represents a NumberSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ------------------------------------------ | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>NumberSchema</code>](#NumberSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>NumberSchema</code>](#NumberSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -947,12 +924,11 @@ <a name="minimum"></a> | ||
## minimum(min) ⇒ <code>FluentSchema</code> | ||
It represents an inclusive lower limit for a numeric instance. | ||
It represents an inclusive lower limit for a numeric instance. | ||
[reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.2.4) | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| min | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| min | [<code>number</code>](#number) | | ||
@@ -962,3 +938,2 @@ <a name="exclusiveMinimum"></a> | ||
## exclusiveMinimum(min) ⇒ <code>FluentSchema</code> | ||
It represents an exclusive lower limit for a numeric instance. | ||
@@ -968,7 +943,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| min | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| min | [<code>number</code>](#number) | | ||
@@ -978,12 +953,11 @@ <a name="maximum"></a> | ||
## maximum(max) ⇒ <code>FluentSchema</code> | ||
It represents an inclusive upper limit for a numeric instance. | ||
It represents an inclusive upper limit for a numeric instance. | ||
[reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.2.2) | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| max | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| max | [<code>number</code>](#number) | | ||
@@ -993,3 +967,2 @@ <a name="exclusiveMaximum"></a> | ||
## exclusiveMaximum(max) ⇒ <code>FluentSchema</code> | ||
It represents an exclusive upper limit for a numeric instance. | ||
@@ -999,7 +972,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| max | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| max | [<code>number</code>](#number) | | ||
@@ -1009,3 +982,2 @@ <a name="multipleOf"></a> | ||
## multipleOf(multiple) ⇒ <code>FluentSchema</code> | ||
It's strictly greater than 0. | ||
@@ -1015,7 +987,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| -------- | ------------------------------ | | ||
| multiple | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| multiple | [<code>number</code>](#number) | | ||
@@ -1025,12 +997,11 @@ <a name="ObjectSchema"></a> | ||
## ObjectSchema([options]) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
Represents a ObjectSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ------------------------------------------ | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -1040,3 +1011,2 @@ <a name="additionalProperties"></a> | ||
## additionalProperties(value) ⇒ <code>FluentSchema</code> | ||
This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself. | ||
@@ -1050,7 +1020,7 @@ Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties", | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------------------------------------- | | ||
| value | <code>FluentSchema</code> \| [<code>boolean</code>](#boolean) | | ||
| Param | Type | | ||
| --- | --- | | ||
| value | <code>FluentSchema</code> \| [<code>boolean</code>](#boolean) | | ||
@@ -1060,3 +1030,2 @@ <a name="maxProperties"></a> | ||
## maxProperties(max) ⇒ <code>FluentSchema</code> | ||
An object instance is valid against "maxProperties" if its number of properties is less than, or equal to, the value of this keyword. | ||
@@ -1066,7 +1035,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| max | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| max | [<code>number</code>](#number) | | ||
@@ -1076,3 +1045,2 @@ <a name="minProperties"></a> | ||
## minProperties(min) ⇒ <code>FluentSchema</code> | ||
An object instance is valid against "minProperties" if its number of properties is greater than, or equal to, the value of this keyword. | ||
@@ -1082,7 +1050,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| min | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| min | [<code>number</code>](#number) | | ||
@@ -1092,3 +1060,2 @@ <a name="patternProperties"></a> | ||
## patternProperties(opts) ⇒ <code>FluentSchema</code> | ||
Each property name of this object SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect. | ||
@@ -1102,7 +1069,7 @@ Each property value of this object MUST be a valid JSON Schema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| opts | [<code>object</code>](#object) | | ||
| Param | Type | | ||
| --- | --- | | ||
| opts | [<code>object</code>](#object) | | ||
@@ -1112,3 +1079,2 @@ <a name="dependencies"></a> | ||
## dependencies(opts) ⇒ <code>FluentSchema</code> | ||
This keyword specifies rules that are evaluated if the instance is an object and contains a certain property. | ||
@@ -1121,12 +1087,39 @@ This keyword's value MUST be an object. Each property specifies a dependency. Each dependency value MUST be an array or a valid JSON Schema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| opts | [<code>object</code>](#object) | | ||
| Param | Type | | ||
| --- | --- | | ||
| opts | [<code>object</code>](#object) | | ||
<a name="dependentRequired"></a> | ||
## dependentRequired(opts) ⇒ <code>FluentSchema</code> | ||
The value of "properties" MUST be an object. Each dependency value MUST be an array. | ||
Each element in the array MUST be a string and MUST be unique. If the dependency key is a property in the instance, each of the items in the dependency value must be a property that exists in the instance. | ||
[reference](https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.5.4) | ||
**Kind**: global function | ||
| Param | Type | | ||
| --- | --- | | ||
| opts | [<code>object</code>](#object) | | ||
<a name="dependentSchemas"></a> | ||
## dependentSchemas(opts) ⇒ <code>FluentSchema</code> | ||
The value of "properties" MUST be an object. The dependency value MUST be a valid JSON Schema. | ||
Each dependency key is a property in the instance and the entire instance must validate against the dependency value. | ||
[reference](https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.2.4) | ||
**Kind**: global function | ||
| Param | Type | | ||
| --- | --- | | ||
| opts | [<code>object</code>](#object) | | ||
<a name="propertyNames"></a> | ||
## propertyNames(value) ⇒ <code>FluentSchema</code> | ||
If the instance is an object, this keyword validates if every property name in the instance validates against the provided schema. | ||
@@ -1137,7 +1130,7 @@ Note the property name that the schema is testing will always be a string. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------- | | ||
| value | <code>FluentSchema</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| value | <code>FluentSchema</code> | | ||
@@ -1147,3 +1140,2 @@ <a name="prop"></a> | ||
## prop(name, props) ⇒ <code>FluentSchema</code> | ||
The value of "properties" MUST be an object. Each value of this object MUST be a valid JSON Schema. | ||
@@ -1153,13 +1145,23 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| name | [<code>string</code>](#string) | | ||
| props | <code>FluentSchema</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| name | [<code>string</code>](#string) | | ||
| props | <code>FluentSchema</code> | | ||
<a name="only"></a> | ||
## only(properties) ⇒ [<code>ObjectSchema</code>](#ObjectSchema) | ||
Returns an object schema with only a subset of keys provided | ||
**Kind**: global function | ||
| Param | Description | | ||
| --- | --- | | ||
| properties | a list of properties you want to keep | | ||
<a name="definition"></a> | ||
## definition(name, props) ⇒ <code>FluentSchema</code> | ||
The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema. | ||
@@ -1170,8 +1172,8 @@ There are no restrictions placed on the values within the array. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| name | [<code>string</code>](#string) | | ||
| props | <code>FluentSchema</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| name | [<code>string</code>](#string) | | ||
| props | <code>FluentSchema</code> | | ||
@@ -1181,10 +1183,9 @@ <a name="RawSchema"></a> | ||
## RawSchema(schema) ⇒ <code>FluentSchema</code> | ||
Represents a raw JSON Schema that will be parsed | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------ | ------------------- | | ||
| schema | <code>Object</code> | | ||
| Param | Type | | ||
| --- | --- | | ||
| schema | <code>Object</code> | | ||
@@ -1194,12 +1195,11 @@ <a name="StringSchema"></a> | ||
## StringSchema([options]) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
Represents a StringSchema. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | Default | Description | | ||
| --------------------- | ------------------------------------------ | ------------------ | -------------------------------------------------- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [options] | <code>Object</code> | | Options | | ||
| [options.schema] | [<code>StringSchema</code>](#StringSchema) | | Default schema | | ||
| [options.generateIds] | [<code>boolean</code>](#boolean) | <code>false</code> | generate the id automatically e.g. #properties.foo | | ||
@@ -1209,3 +1209,2 @@ <a name="minLength"></a> | ||
## minLength(min) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword. | ||
@@ -1216,7 +1215,7 @@ The length of a string instance is defined as the number of its characters as defined by RFC 7159 [RFC7159]. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| min | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| min | [<code>number</code>](#number) | | ||
@@ -1226,3 +1225,2 @@ <a name="maxLength"></a> | ||
## maxLength(max) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword. | ||
@@ -1233,7 +1231,7 @@ The length of a string instance is defined as the number of its characters as defined by RFC 7159 [RFC7159]. | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ----- | ------------------------------ | | ||
| max | [<code>number</code>](#number) | | ||
| Param | Type | | ||
| --- | --- | | ||
| max | [<code>number</code>](#number) | | ||
@@ -1243,3 +1241,2 @@ <a name="format"></a> | ||
## format(format) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
A string value can be RELATIVE_JSON_POINTER, JSON_POINTER, UUID, REGEX, IPV6, IPV4, HOSTNAME, EMAIL, URL, URI_TEMPLATE, URI_REFERENCE, URI, TIME, DATE, | ||
@@ -1249,7 +1246,7 @@ | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------ | ------------------------------ | | ||
| format | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| format | [<code>string</code>](#string) | | ||
@@ -1259,13 +1256,12 @@ <a name="pattern"></a> | ||
## pattern(pattern) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
This string SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect. | ||
A string instance is considered valid if the regular expression matches the instance successfully. | ||
A string instance is considered valid if the regular expression matches the instance successfully. | ||
[reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.6.3.3) | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| ------- | ------------------------------ | | ||
| pattern | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| pattern | [<code>string</code>](#string) | | ||
@@ -1275,14 +1271,13 @@ <a name="contentEncoding"></a> | ||
## contentEncoding(encoding) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
If the instance value is a string, this property defines that the string SHOULD | ||
be interpreted as binary data and decoded using the encoding named by this property. | ||
RFC 2045, Sec 6.1 [RFC2045] lists the possible values for this property. | ||
be interpreted as binary data and decoded using the encoding named by this property. | ||
RFC 2045, Sec 6.1 [RFC2045] lists the possible values for this property. | ||
[reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.8.3) | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| -------- | ------------------------------ | | ||
| encoding | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| encoding | [<code>string</code>](#string) | | ||
@@ -1292,12 +1287,12 @@ <a name="contentMediaType"></a> | ||
## contentMediaType(mediaType) ⇒ [<code>StringSchema</code>](#StringSchema) | ||
The value of this property must be a media type, as defined by RFC 2046 [RFC2046]. | ||
This property defines the media type of instances which this schema defines. | ||
This property defines the media type of instances which this schema defines. | ||
[reference](https://tools.ietf.org/id/draft-handrews-json-schema-validation-01.html#rfc.section.8.4) | ||
**Kind**: global function | ||
**Kind**: global function | ||
| Param | Type | | ||
| --------- | ------------------------------ | | ||
| mediaType | [<code>string</code>](#string) | | ||
| Param | Type | | ||
| --- | --- | | ||
| mediaType | [<code>string</code>](#string) | | ||
{ | ||
"name": "fluent-json-schema", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "JSON Schema fluent API", | ||
@@ -66,4 +66,4 @@ "main": "src/FluentJSONSchema.js", | ||
"ajv-keywords": "^3.4.1", | ||
"husky": "^6.0.0", | ||
"jest": "^26.6.3", | ||
"husky": "^7.0.0", | ||
"jest": "^27.0.1", | ||
"jsdoc-to-markdown": "^7.0.0", | ||
@@ -70,0 +70,0 @@ "lint-staged": "^11.0.0", |
@@ -198,5 +198,5 @@ 'use strict' | ||
: [REQUIRED] | ||
if(!isUniq(required)){ | ||
throw new FluentSchemaError("'required' has repeated keys, check your calls to require()") | ||
throw new FluentSchemaError("'required' has repeated keys, check your calls to .required()") | ||
} | ||
@@ -410,6 +410,13 @@ | ||
* | ||
* @param {Object} [options] - Options | ||
* @param {boolean} [options.isRoot = true] - Is a root level schema | ||
* @returns {object} | ||
*/ | ||
valueOf: () => { | ||
valueOf: ({ isRoot } = { isRoot: true }) => { | ||
const { properties, definitions, required, $schema, ...rest } = schema | ||
if (isRoot && required && !required.every((v) => typeof v === 'string')) { | ||
throw new FluentSchemaError("'required' has called on root-level schema, check your calls to .required()") | ||
} | ||
return Object.assign( | ||
@@ -416,0 +423,0 @@ $schema ? { $schema } : {}, |
@@ -164,3 +164,3 @@ const { BaseSchema } = require('./BaseSchema') | ||
new S.FluentSchemaError( | ||
"'required' has repeated keys, check your calls to require()" | ||
"'required' has repeated keys, check your calls to .required()" | ||
) | ||
@@ -176,3 +176,3 @@ ) | ||
new S.FluentSchemaError( | ||
"'required' has repeated keys, check your calls to require()" | ||
"'required' has repeated keys, check your calls to .required()" | ||
) | ||
@@ -183,2 +183,12 @@ ) | ||
it('root-level required', () => { | ||
expect(() => { | ||
return S.object().required().valueOf() | ||
}).toThrowError( | ||
new S.FluentSchemaError( | ||
"'required' has called on root-level schema, check your calls to .required()" | ||
) | ||
) | ||
}) | ||
describe('array', () => { | ||
@@ -185,0 +195,0 @@ it('simple', () => { |
@@ -25,3 +25,3 @@ export interface BaseSchema<T> { | ||
isFluentJSONSchema: boolean | ||
raw: (fragment: any) => JSONSchema | ||
raw: (fragment: any) => T | ||
} | ||
@@ -128,2 +128,4 @@ | ||
only: (properties: string[]) => ObjectSchema | ||
dependentRequired: (options: DependentRequiredOptions) => ObjectSchema | ||
dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema | ||
} | ||
@@ -222,2 +224,11 @@ | ||
} | ||
interface DependentSchemaOptions { | ||
[key: string]: JSONSchema | ||
} | ||
interface DependentRequiredOptions { | ||
[key: string]: string[] | ||
} | ||
export function withOptions<T>(options: SchemaOptions): T | ||
@@ -245,3 +256,3 @@ | ||
) => MixedSchema<T> | ||
raw: (fragment: any) => JSONSchema | ||
raw: (fragment: any) => S | ||
FORMATS: FORMATS | ||
@@ -248,0 +259,0 @@ } |
@@ -61,3 +61,3 @@ 'use strict' | ||
if (isFluentSchema(value)) { | ||
const { $schema, ...rest } = value.valueOf() | ||
const { $schema, ...rest } = value.valueOf({ isRoot: false }) | ||
return setAttribute({ schema, ...options }, [ | ||
@@ -131,3 +131,3 @@ 'additionalProperties', | ||
...memo, | ||
[pattern]: omit(schema.valueOf(), ['$schema']), | ||
[pattern]: omit(schema.valueOf({ isRoot: false }), ['$schema']), | ||
} | ||
@@ -157,3 +157,3 @@ }, {}) | ||
throw new FluentSchemaError( | ||
"'dependencies' invalid options. Provide a valid map e.g. { 'foo': ['ba'] } or { 'foo': S.string() }" | ||
"'dependencies' invalid options. Provide a valid map e.g. { 'foo': ['bar'] } or { 'foo': S.string() }" | ||
) | ||
@@ -164,3 +164,3 @@ return { | ||
? schema | ||
: omit(schema.valueOf(), ['$schema', 'type', 'definitions']), | ||
: omit(schema.valueOf({ isRoot: false }), ['$schema', 'type', 'definitions']), | ||
} | ||
@@ -176,2 +176,58 @@ }, {}) | ||
/** | ||
* The value of "properties" MUST be an object. Each dependency value MUST be an array. | ||
* Each element in the array MUST be a string and MUST be unique. If the dependency key is a property in the instance, each of the items in the dependency value must be a property that exists in the instance. | ||
* | ||
* {@link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.5.4|reference} | ||
* @param {object} opts | ||
* @returns {FluentSchema} | ||
*/ | ||
dependentRequired: opts => { | ||
const values = Object.entries(opts).reduce((memo, [prop, schema]) => { | ||
if (!Array.isArray(schema)) | ||
throw new FluentSchemaError( | ||
"'dependentRequired' invalid options. Provide a valid array e.g. { 'foo': ['bar'] }" | ||
) | ||
return { | ||
...memo, | ||
[prop]: schema, | ||
} | ||
}, {}) | ||
return setAttribute({ schema, ...options }, [ | ||
'dependentRequired', | ||
values, | ||
'object', | ||
]) | ||
}, | ||
/** | ||
* The value of "properties" MUST be an object. The dependency value MUST be a valid JSON Schema. | ||
* Each dependency key is a property in the instance and the entire instance must validate against the dependency value. | ||
* | ||
* {@link https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.9.2.2.4|reference} | ||
* @param {object} opts | ||
* @returns {FluentSchema} | ||
*/ | ||
dependentSchemas: opts => { | ||
const values = Object.entries(opts).reduce((memo, [prop, schema]) => { | ||
if (!isFluentSchema(schema)) | ||
throw new FluentSchemaError( | ||
"'dependentSchemas' invalid options. Provide a valid schema e.g. { 'foo': S.string() }" | ||
) | ||
return { | ||
...memo, | ||
[prop]: omit(schema.valueOf({ isRoot: false }), ['$schema', 'type', 'definitions']), | ||
} | ||
}, {}) | ||
return setAttribute({ schema, ...options }, [ | ||
'dependentSchemas', | ||
values, | ||
'object', | ||
]) | ||
}, | ||
/** | ||
* If the instance is an object, this keyword validates if every property name in the instance validates against the provided schema. | ||
@@ -190,3 +246,3 @@ * Note the property name that the schema is testing will always be a string. | ||
'propertyNames', | ||
omit(value.valueOf(), ['$schema']), | ||
omit(value.valueOf({ isRoot: false }), ['$schema']), | ||
'object', | ||
@@ -213,3 +269,3 @@ ]) | ||
const target = props.def ? 'definitions' : 'properties' | ||
let attributes = props.valueOf() | ||
let attributes = props.valueOf({ isRoot: false }) | ||
const $id = | ||
@@ -320,3 +376,3 @@ attributes.$id || | ||
ObjectSchema({ schema, ...options }).prop(name, { | ||
...props.valueOf(), | ||
...props.valueOf({ isRoot: false }), | ||
def: true, | ||
@@ -323,0 +379,0 @@ }), |
@@ -504,3 +504,3 @@ const { ObjectSchema } = require('./ObjectSchema') | ||
new S.FluentSchemaError( | ||
"'dependencies' invalid options. Provide a valid map e.g. { 'foo': ['ba'] } or { 'foo': S.string() }" | ||
"'dependencies' invalid options. Provide a valid map e.g. { 'foo': ['bar'] } or { 'foo': S.string() }" | ||
) | ||
@@ -511,2 +511,84 @@ ) | ||
describe('dependentRequired', () => { | ||
it('valid', () => { | ||
expect( | ||
ObjectSchema() | ||
.dependentRequired({ | ||
foo: ['bar'], | ||
}) | ||
.prop('foo') | ||
.prop('bar') | ||
.valueOf() | ||
).toEqual({ | ||
type: 'object', | ||
dependentRequired: { | ||
foo: [ 'bar' ] | ||
}, | ||
properties: { | ||
foo: {}, | ||
bar: {} | ||
} | ||
}) | ||
}) | ||
it('invalid', () => { | ||
const value = { | ||
foo: ObjectSchema().prop('bar', S.string()), | ||
} | ||
expect(() => { | ||
expect( | ||
ObjectSchema() | ||
.dependentRequired(value) | ||
.prop('foo') | ||
).toEqual(value) | ||
}).toThrowError( | ||
new S.FluentSchemaError( | ||
"'dependentRequired' invalid options. Provide a valid array e.g. { 'foo': ['bar'] }" | ||
) | ||
) | ||
}) | ||
}) | ||
describe('dependentSchemas', () => { | ||
it('valid', () => { | ||
expect( | ||
ObjectSchema() | ||
.dependentSchemas({ | ||
foo: ObjectSchema().prop('bar', S.string()) | ||
}) | ||
.prop('foo') | ||
.valueOf() | ||
).toEqual({ | ||
dependentSchemas: { | ||
foo: { | ||
properties: { | ||
bar: { type: 'string' }, | ||
}, | ||
}, | ||
}, | ||
properties: { foo: {} }, | ||
type: 'object', | ||
}) | ||
}) | ||
it('invalid', () => { | ||
const value = { | ||
foo: ['bar'] | ||
} | ||
expect(() => { | ||
expect( | ||
ObjectSchema() | ||
.dependentSchemas(value) | ||
.prop('foo') | ||
).toEqual(value) | ||
}).toThrowError( | ||
new S.FluentSchemaError( | ||
"'dependentSchemas' invalid options. Provide a valid schema e.g. { 'foo': S.string() }" | ||
) | ||
) | ||
}) | ||
}) | ||
describe('propertyNames', () => { | ||
@@ -513,0 +595,0 @@ it('valid', () => { |
@@ -24,5 +24,3 @@ // This file will be passed to the TypeScript CLI to verify our typings compile | ||
'avatar', | ||
S.string() | ||
.contentEncoding('base64') | ||
.contentMediaType('image/png') | ||
S.string().contentEncoding('base64').contentMediaType('image/png') | ||
) | ||
@@ -32,7 +30,3 @@ .required() | ||
'password', | ||
S.string() | ||
.default('123456') | ||
.minLength(6) | ||
.maxLength(12) | ||
.pattern('.*') | ||
S.string().default('123456').minLength(6).maxLength(12).pattern('.*') | ||
) | ||
@@ -73,1 +67,20 @@ .required() | ||
console.log('user:\n', JSON.stringify(userSchema)) | ||
const dependentRequired = S.object() | ||
.dependentRequired({ | ||
foo: ['bar'], | ||
}) | ||
.prop('foo') | ||
.prop('bar') | ||
.valueOf() | ||
console.log('dependentRequired:\n', JSON.stringify(dependentRequired)) | ||
const dependentSchemas = S.object() | ||
.dependentSchemas({ | ||
foo: S.object().prop('bar'), | ||
}) | ||
.prop('foo', S.object().prop('bar')) | ||
.valueOf() | ||
console.log('dependentRequired:\n', JSON.stringify(dependentSchemas)) |
@@ -93,1 +93,28 @@ // This file will be passed to the TypeScript CLI to verify our typings compile | ||
console.log('extend of user\n', JSON.stringify(extendExtendedSchema)) | ||
const rawNullableSchema = S.object() | ||
.raw({ nullable: true }) | ||
.required(['foo', 'hello']) | ||
.prop('foo', S.string()) | ||
.prop('hello', S.string()) | ||
console.log('raw schema with nullable props\n', JSON.stringify(rawNullableSchema)) | ||
const dependentRequired = S.object() | ||
.dependentRequired({ | ||
foo: ['bar'], | ||
}) | ||
.prop('foo') | ||
.prop('bar') | ||
.valueOf() | ||
console.log('dependentRequired:\n', JSON.stringify(dependentRequired)) | ||
const dependentSchemas = S.object() | ||
.dependentSchemas({ | ||
foo: S.object().prop('bar'), | ||
}) | ||
.prop('foo', S.object().prop('bar')) | ||
.valueOf() | ||
console.log('dependentRequired:\n', JSON.stringify(dependentSchemas)) |
@@ -161,3 +161,3 @@ 'use strict' | ||
if(!isUniq(patchedRequired)){ | ||
throw new FluentSchemaError("'required' has repeated keys, check your calls to require()") | ||
throw new FluentSchemaError("'required' has repeated keys, check your calls to .required()") | ||
} | ||
@@ -209,3 +209,3 @@ | ||
const values = schemas.map(schema => { | ||
const { $schema, ...props } = schema.valueOf() | ||
const { $schema, ...props } = schema.valueOf({ isRoot: false }) | ||
return props | ||
@@ -212,0 +212,0 @@ }) |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
254166
0.6%6246
2.93%