Skip to content

Commit

Permalink
fixes: object schema carry properties added by raw to $ref (#223)
Browse files Browse the repository at this point in the history
* fixes: object schema carry properties added by raw to $ref

fixes #216

* Apply suggestions from code review

* update to spread operator for consistency
  • Loading branch information
dancastillo authored Jul 26, 2023
1 parent 7137bf3 commit 6f06632
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
* @param {string} id - an #id
**/
id: id => {
if (!id)
if (!id) {
throw new FluentSchemaError(
`id should not be an empty fragment <#> or an empty string <> (e.g. #myId)`
'id should not be an empty fragment <#> or an empty string <> (e.g. #myId)'
)
}
return options.factory({ schema: { ...schema, $id: id }, ...options })
},
/**
Expand Down Expand Up @@ -277,8 +278,10 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
}
const target = props.def ? 'definitions' : 'properties'
let attributes = props.valueOf({ isRoot: false })

const { $ref, $id: attributeId, required, ...restAttributes } = attributes
const $id =
attributes.$id ||
attributeId ||
(options.generateIds ? `#${target}/${name}` : undefined)
if (isFluentSchema(props)) {
attributes = patchIdsWithParentId({
Expand All @@ -303,8 +306,6 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
? undefined
: attributes.type

const $ref = attributes.$ref

// strip undefined values or empty arrays or internals
attributes = Object.entries({ ...attributes, $id, type }).reduce(
(memo, [key, value]) => {
Expand All @@ -323,7 +324,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
...schema,
[target]: [
...schema[target],
$ref ? { name, $ref } : Object.assign({}, { name }, attributes)
$ref ? { name, $ref, ...restAttributes } : { name, ...attributes }
]
},
...options
Expand Down
25 changes: 24 additions & 1 deletion src/ObjectSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('ObjectSchema', () => {
.valueOf()
).toEqual({
$id: id,
properties: {'prop': {}},
properties: { prop: {} },
type: 'object'
})
})
Expand Down Expand Up @@ -1034,5 +1034,28 @@ describe('ObjectSchema', () => {
customKeyword: true
})
})

it('Carry raw properties', () => {
const schema = S.object()
.prop('test', S.ref('foo').raw({ test: true }))
.valueOf()
expect(schema).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: { test: { $ref: 'foo', test: true } }
})
})

it('Carry raw properties multiple props', () => {
const schema = S.object()
.prop('a', S.string())
.prop('test', S.ref('foo').raw({ test: true }))
.valueOf()
expect(schema).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: { a: { type: 'string' }, test: { $ref: 'foo', test: true } }
})
})
})
})

0 comments on commit 6f06632

Please sign in to comment.