Skip to content

Commit

Permalink
adding deprecated function to BaseSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Tostes committed Jan 12, 2022
1 parent 747f40e commit cde0f76
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/BaseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
omit,
isFluentSchema,
last,
isBoolean,
isUniq,
patchIdsWithParentId,
REQUIRED,
Expand Down Expand Up @@ -178,6 +179,20 @@ const BaseSchema = (
return setAttribute({ schema, ...options }, ['writeOnly', value, 'boolean'])
},

/**
* 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:
* Examples:
Expand Down
165 changes: 165 additions & 0 deletions src/BaseSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,171 @@ describe('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', () => {
it('valid', () => {
const value = ['VALUE']
Expand Down
1 change: 0 additions & 1 deletion src/MixedSchema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict'
const { BaseSchema } = require('./BaseSchema')
const { NullSchema } = require('./NullSchema')
const { BooleanSchema } = require('./BooleanSchema')
const { StringSchema } = require('./StringSchema')
Expand Down
2 changes: 1 addition & 1 deletion src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
return ObjectSchema({
schema: {
...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)),
},
...options,
Expand Down
3 changes: 3 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const last = array => {

const isUniq = array => array.filter((v, i, a) => a.indexOf(v) === i).length === array.length;

const isBoolean = value => 'boolean' === typeof value;

const omit = (obj, props) =>
Object.entries(obj).reduce((memo, [key, value]) => {
if (props.includes(key)) return memo
Expand Down Expand Up @@ -218,6 +220,7 @@ module.exports = {
FluentSchemaError,
last,
isUniq,
isBoolean,
flat,
toArray,
omit,
Expand Down

0 comments on commit cde0f76

Please sign in to comment.