Skip to content

Commit

Permalink
fixes: object schema carry properties added by raw to $ref
Browse files Browse the repository at this point in the history
fixes #216
  • Loading branch information
dancastillo committed Jul 22, 2023
1 parent 7137bf3 commit a8c4a6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
15 changes: 9 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,9 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
...schema,
[target]: [
...schema[target],
$ref ? { name, $ref } : Object.assign({}, { name }, attributes)
$ref
? (Object.keys(restAttributes).length ? { name, $ref, ...restAttributes } : { name, $ref })
: Object.assign({}, { 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 a8c4a6c

Please sign in to comment.