Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding deprecated function to BaseSchema #161

Merged
merged 2 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions src/FluentJSONSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface BaseSchema<T> {
oneOf: (schema: Array<JSONSchema>) => T
readOnly: (isReadOnly?: boolean) => T
writeOnly: (isWriteOnly?: boolean) => T
deprecated: (isDeprecated?: boolean) => T
isFluentSchema: boolean
isFluentJSONSchema: boolean
raw: (fragment: any) => T
Expand Down Expand Up @@ -124,6 +125,7 @@ export interface ObjectSchema extends BaseSchema<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
}
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
18 changes: 18 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ const userSubsetSchema = largeUserSchema.only(['username', 'password'])

console.log('user subset:', JSON.stringify(userSubsetSchema.valueOf()))

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 {
S.object().prop('foo', 'boom!' as any)
} catch (e) {
Expand Down Expand Up @@ -117,3 +128,10 @@ const dependentSchemas = S.object()
.valueOf()

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))
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