Skip to content

Commit

Permalink
fix(objectschema)!: omit id from schema returned by without + only
Browse files Browse the repository at this point in the history
the methods without and only return new schema objects with the same id
that was found on the original. In terms of schema reuse, this creates
two different schemas with the same id which is technically invalid.
Additionally, it was not possible to set the id property on an object
schema that had properties defined. This problem originates from the
setattribute function which will always set the attribute on the
schema properties if they are defined. In the case of the object schema,
this is almost always the case. This changes the id function on the
object schema to always generate a new schema with the id set on the
object schema rather than its properties

BREAKING CHANGE: ObjectSchema.id() will always set the id on the root object
BREAKING CHANGE: ObjectSchema.without() will omit id from the return schema
BREAKING CHANGE: ObjectSchema.only() will omit id from the return schema
  • Loading branch information
esatterwhite committed Sep 24, 2022
1 parent 490c24d commit 239fe77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
return {
...BaseSchema({ ...options, schema }),

id: id => {
if (!id)
throw new FluentSchemaError(
`id should not be an empty fragment <#> or an empty string <> (e.g. #myId)`
)
return options.factory({ schema: { ...schema, $id: id }, ...options })
},
/**
* This keyword determines how child instances validate for objects, and does not directly validate the immediate instance itself.
* Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties",
Expand Down Expand Up @@ -341,7 +348,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
only: properties => {
return ObjectSchema({
schema: {
...schema,
...omit(schema, ['$id', 'properties']),
properties: schema.properties.filter(({ name }) => properties.includes(name)),
required: schema.required.filter(p => properties.includes(p)),
},
Expand All @@ -358,7 +365,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
without: properties => {
return ObjectSchema({
schema: {
...schema,
...omit(schema, ['$id', 'properties']),
properties: schema.properties.filter(({ name }) => !properties.includes(name)),
required: schema.required.filter(p => !properties.includes(p)),
},
Expand Down
18 changes: 12 additions & 6 deletions src/ObjectSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ describe('ObjectSchema', () => {
type: 'object',
})
})
it('invalid', () => {
expect(() => {
ObjectSchema().id('')
}).toThrowError(
new S.FluentSchemaError(
'id should not be an empty fragment <#> or an empty string <> (e.g. #myId)'
)
)
})
})
})

Expand Down Expand Up @@ -213,14 +222,15 @@ describe('ObjectSchema', () => {
describe('id', () => {
it('valid', () => {
const id = 'myId'
const prop = 'prop'
expect(
ObjectSchema()
.prop('prop')
.id(id)
.valueOf().properties[prop]
.valueOf()
).toEqual({
$id: id,
properties: {'prop': {}},
type: 'object'
})
})

Expand Down Expand Up @@ -915,7 +925,6 @@ describe('ObjectSchema', () => {

expect(only.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'base',
title: 'base',
properties: {
foo: {
Expand All @@ -939,7 +948,6 @@ describe('ObjectSchema', () => {

expect(only.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'base',
title: 'base',
properties: {
foo: {
Expand Down Expand Up @@ -974,7 +982,6 @@ describe('ObjectSchema', () => {

expect(without.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'base',
title: 'base',
properties: {
bar: {
Expand All @@ -1001,7 +1008,6 @@ describe('ObjectSchema', () => {

expect(without.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'base',
title: 'base',
properties: {
baz: {
Expand Down

0 comments on commit 239fe77

Please sign in to comment.