fluent-json-schema
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -85,2 +85,7 @@ ## Functions | ||
</dd> | ||
<dt><a href="#deprecated">deprecated(isDeprecated)</a> ⇒ <code><a href="#BaseSchema">BaseSchema</a></code></dt> | ||
<dd><p>The value of deprecated can be left empty to indicate the property is deprecated. | ||
It takes an optional boolean which can be used to explicitly set deprecated true/false.</p> | ||
<p><a href="https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9.3">reference</a></p> | ||
</dd> | ||
<dt><a href="#required">required()</a> ⇒ <code>FluentSchema</code></dt> | ||
@@ -277,2 +282,5 @@ <dd><p>Required has to be chained to a property: | ||
</dd> | ||
<dt><a href="#without">without(properties)</a> ⇒ <code><a href="#ObjectSchema">ObjectSchema</a></code></dt> | ||
<dd><p>Returns an object schema without a subset of keys provided</p> | ||
</dd> | ||
<dt><a href="#definition">definition(name, props)</a> ⇒ <code>FluentSchema</code></dt> | ||
@@ -565,2 +573,16 @@ <dd><p>The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema. | ||
<a name="deprecated"></a> | ||
## deprecated(isDeprecated) ⇒ [<code>BaseSchema</code>](#BaseSchema) | ||
The value of deprecated can be left empty to indicate the property is deprecated. | ||
It takes an optional boolean which can be used to explicitly set deprecated true/false. | ||
[reference](https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9.3) | ||
**Kind**: global function | ||
| Param | Type | | ||
| --- | --- | | ||
| isDeprecated | <code>Boolean</code> | | ||
<a name="required"></a> | ||
@@ -1078,2 +1100,13 @@ | ||
<a name="without"></a> | ||
## without(properties) ⇒ [<code>ObjectSchema</code>](#ObjectSchema) | ||
Returns an object schema without a subset of keys provided | ||
**Kind**: global function | ||
| Param | Description | | ||
| --- | --- | | ||
| properties | a list of properties you dont want to keep | | ||
<a name="definition"></a> | ||
@@ -1080,0 +1113,0 @@ |
{ | ||
"name": "fluent-json-schema", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "JSON Schema fluent API", | ||
@@ -69,3 +69,3 @@ "main": "src/FluentJSONSchema.js", | ||
"jsdoc-to-markdown": "^7.0.0", | ||
"lint-staged": "^11.0.0", | ||
"lint-staged": "^12.0.2", | ||
"lodash.merge": "^4.6.2", | ||
@@ -72,0 +72,0 @@ "prettier": "^2.2.1", |
@@ -298,3 +298,3 @@ # fluent-json-schema | ||
## Selecting only certain properties of your schema | ||
## Selecting certain properties of your schema | ||
@@ -304,2 +304,4 @@ In addition to extending schemas, it is also possible to reduce them into smaller schemas. This comes in handy | ||
Select only properties you want to keep. | ||
```js | ||
@@ -317,2 +319,16 @@ const S = require('fluent-json-schema') | ||
Or remove properties you dont want to keep. | ||
```js | ||
const S = require('fluent-json-schema') | ||
const personSchema = S.object() | ||
.prop('name', S.string()) | ||
.prop('age', S.number()) | ||
.prop('id', S.string().format('uuid')) | ||
.prop('createdAt', S.string().format('time')) | ||
.prop('updatedAt', S.string().format('time')) | ||
const bodySchema = personSchema.without(['createdAt', 'updatedAt']) | ||
``` | ||
### Detect Fluent Schema objects | ||
@@ -319,0 +335,0 @@ |
@@ -7,2 +7,3 @@ 'use strict' | ||
last, | ||
isBoolean, | ||
isUniq, | ||
@@ -183,2 +184,16 @@ patchIdsWithParentId, | ||
/** | ||
* The value of deprecated can be left empty to indicate the property is deprecated. | ||
* It takes an optional boolean which can be used to explicitly set deprecated true/false. | ||
* | ||
* {@link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9.3|reference} | ||
* @param {Boolean} isDeprecated | ||
* @returns {BaseSchema} | ||
*/ | ||
deprecated: (isDeprecated) => { | ||
if(isDeprecated && !isBoolean(isDeprecated)) throw new FluentSchemaError("'deprecated' must be a boolean value") | ||
const value = isDeprecated !== undefined ? isDeprecated : true | ||
return setAttribute({ schema, ...options }, ['deprecated', value, 'boolean']) | ||
}, | ||
/** | ||
* Required has to be chained to a property: | ||
@@ -185,0 +200,0 @@ * Examples: |
@@ -215,2 +215,167 @@ const { BaseSchema } = require('./BaseSchema') | ||
describe('deprecated', () => { | ||
it('valid', () => { | ||
expect( | ||
BaseSchema() | ||
.deprecated(true) | ||
.valueOf().deprecated | ||
).toEqual(true) | ||
}) | ||
it('invalid', () => { | ||
expect( | ||
() => | ||
BaseSchema() | ||
.deprecated('somethingNotBoolean') | ||
.valueOf().deprecated | ||
).toThrowError( | ||
new S.FluentSchemaError( | ||
"'deprecated' must be a boolean value" | ||
) | ||
) | ||
}) | ||
it('valid with no value', () => { | ||
expect( | ||
BaseSchema() | ||
.deprecated() | ||
.valueOf().deprecated | ||
).toEqual(true) | ||
}) | ||
it('can be set to false', () => { | ||
expect( | ||
BaseSchema() | ||
.deprecated(false) | ||
.valueOf().deprecated | ||
).toEqual(false) | ||
}) | ||
it('property', () => { | ||
expect( | ||
S.object() | ||
.prop('foo', S.string()) | ||
.prop('bar', S.string().deprecated()) | ||
.valueOf() | ||
).toEqual({ | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
properties: { | ||
bar: { type: 'string', deprecated: true }, | ||
foo: { type: 'string' } | ||
}, | ||
type: 'object', | ||
}) | ||
}); | ||
it('object', () => { | ||
expect( | ||
S.object() | ||
.prop('foo', S.string()) | ||
.prop('bar', S | ||
.object() | ||
.deprecated() | ||
.prop('raz', S.string()) | ||
.prop('iah', S.number())) | ||
.valueOf() | ||
).toEqual({ | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
properties: { | ||
foo: { type: 'string' }, | ||
bar: { | ||
type: 'object', | ||
deprecated: true, | ||
properties: { | ||
raz: { type: 'string' }, | ||
iah: { type: 'number' } | ||
}, | ||
}, | ||
}, | ||
type: 'object', | ||
}) | ||
}); | ||
it('object property', () => { | ||
expect( | ||
S.object() | ||
.prop('foo', S.string()) | ||
.prop('bar', S | ||
.object() | ||
.prop('raz', S.string().deprecated()) | ||
.prop('iah', S.number()) | ||
) | ||
.valueOf() | ||
).toEqual({ | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
properties: { | ||
foo: { type: 'string' }, | ||
bar: { | ||
type: 'object', | ||
properties: { | ||
raz: { type: 'string', deprecated: true }, | ||
iah: { type: 'number' } | ||
}, | ||
}, | ||
}, | ||
type: 'object', | ||
}) | ||
}); | ||
it('array', () => { | ||
expect( | ||
S.object() | ||
.prop('foo', S.string()) | ||
.prop('bar', S | ||
.array() | ||
.deprecated() | ||
.items(S.number()) | ||
) | ||
.valueOf() | ||
).toEqual({ | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' }, | ||
bar: { | ||
type: 'array', | ||
deprecated: true, | ||
items: { type: 'number' } | ||
} | ||
}, | ||
}) | ||
}); | ||
it('array item', () => { | ||
expect( | ||
S.object() | ||
.prop('foo', S.string()) | ||
.prop('bar', S | ||
.array() | ||
.items([ | ||
S.object().prop('zoo', S.string()).prop('biz', S.string()), | ||
S.object().deprecated().prop('zal', S.string()).prop('boz', S.string()) | ||
]) | ||
) | ||
.valueOf() | ||
).toEqual({ | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
properties: { | ||
foo: { type: 'string' }, | ||
bar: { | ||
type: 'array', | ||
items: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
zoo: { type: 'string' }, | ||
biz: { type: 'string' } | ||
} | ||
}, | ||
{ | ||
type: 'object', | ||
deprecated: true, | ||
properties: { | ||
zal: { type: 'string' }, | ||
boz: { type: 'string' } | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
}) | ||
}); | ||
}) | ||
describe('enum', () => { | ||
@@ -217,0 +382,0 @@ it('valid', () => { |
@@ -23,2 +23,3 @@ export interface BaseSchema<T> { | ||
writeOnly: (isWriteOnly?: boolean) => T | ||
deprecated: (isDeprecated?: boolean) => T | ||
isFluentSchema: boolean | ||
@@ -128,2 +129,3 @@ isFluentJSONSchema: boolean | ||
only: (properties: string[]) => ObjectSchema | ||
without: (properties: string[]) => ObjectSchema | ||
dependentRequired: (options: DependentRequiredOptions) => ObjectSchema | ||
@@ -130,0 +132,0 @@ dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema |
'use strict' | ||
const { BaseSchema } = require('./BaseSchema') | ||
const { NullSchema } = require('./NullSchema') | ||
@@ -4,0 +3,0 @@ const { BooleanSchema } = require('./BooleanSchema') |
@@ -347,5 +347,3 @@ 'use strict' | ||
...schema, | ||
properties: schema.properties.filter(p => | ||
properties.includes(p.name) | ||
), | ||
properties: schema.properties.filter(({ name }) => properties.includes(name)), | ||
required: schema.required.filter(p => properties.includes(p)), | ||
@@ -358,2 +356,19 @@ }, | ||
/** | ||
* Returns an object schema without a subset of keys provided | ||
* | ||
* @param properties a list of properties you dont want to keep | ||
* @returns {ObjectSchema} | ||
*/ | ||
without: properties => { | ||
return ObjectSchema({ | ||
schema: { | ||
...schema, | ||
properties: schema.properties.filter(({ name }) => !properties.includes(name)), | ||
required: schema.required.filter(p => !properties.includes(p)), | ||
}, | ||
...options, | ||
}) | ||
}, | ||
/** | ||
* The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema. | ||
@@ -360,0 +375,0 @@ * There are no restrictions placed on the values within the array. |
@@ -948,2 +948,64 @@ const { ObjectSchema } = require('./ObjectSchema') | ||
describe('without', () => { | ||
it('returns a subset of the object', () => { | ||
const base = S.object() | ||
.id('base') | ||
.title('base') | ||
.prop('foo', S.string()) | ||
.prop('bar', S.string()) | ||
.prop('baz', S.string()) | ||
.prop( | ||
'children', | ||
S.object() | ||
.prop('alpha', S.string()) | ||
.prop('beta', S.string()) | ||
) | ||
const without = base.without(['foo', 'children']) | ||
expect(without.valueOf()).toEqual({ | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
$id: 'base', | ||
title: 'base', | ||
properties: { | ||
bar: { | ||
type: 'string', | ||
}, | ||
baz: { | ||
type: 'string', | ||
}, | ||
}, | ||
type: 'object', | ||
}) | ||
}) | ||
it('works correctly with required properties', () => { | ||
const base = S.object() | ||
.id('base') | ||
.title('base') | ||
.prop('foo', S.string().required()) | ||
.prop('bar', S.string()) | ||
.prop('baz', S.string().required()) | ||
.prop('qux', S.string()) | ||
const without = base.without(['foo', 'bar']) | ||
expect(without.valueOf()).toEqual({ | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
$id: 'base', | ||
title: 'base', | ||
properties: { | ||
baz: { | ||
type: 'string', | ||
}, | ||
qux: { | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['baz'], | ||
type: 'object', | ||
}) | ||
}) | ||
}) | ||
describe('raw', () => { | ||
@@ -950,0 +1012,0 @@ it('allows to add a custom attribute', () => { |
@@ -78,2 +78,13 @@ // This file will be passed to the TypeScript CLI to verify our typings compile | ||
const personSchema = S.object() | ||
.prop('name', S.string()) | ||
.prop('age', S.number()) | ||
.prop('id', S.string().format('uuid')) | ||
.prop('createdAt', S.string().format('time')) | ||
.prop('updatedAt', S.string().format('time')) | ||
const bodySchema = personSchema.without(['createdAt', 'updatedAt']) | ||
console.log('person subset:', JSON.stringify(bodySchema.valueOf())) | ||
try { | ||
@@ -121,1 +132,8 @@ S.object().prop('foo', 'boom!' as any) | ||
console.log('dependentRequired:\n', JSON.stringify(dependentSchemas)) | ||
const deprecatedSchema = S.object() | ||
.deprecated() | ||
.prop('foo', S.string().deprecated()) | ||
.valueOf() | ||
console.log('deprecatedSchema:\n', JSON.stringify(deprecatedSchema)) |
@@ -23,2 +23,4 @@ 'use strict' | ||
const isBoolean = value => 'boolean' === typeof value; | ||
const omit = (obj, props) => | ||
@@ -222,2 +224,3 @@ Object.entries(obj).reduce((memo, [key, value]) => { | ||
isUniq, | ||
isBoolean, | ||
flat, | ||
@@ -224,0 +227,0 @@ toArray, |
Sorry, the diff of this file is not supported yet
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
263779
6510
360