Skip to content

Commit 6f06632

Browse files
authored
fixes: object schema carry properties added by raw to $ref (#223)
* fixes: object schema carry properties added by raw to $ref fixes #216 * Apply suggestions from code review * update to spread operator for consistency
1 parent 7137bf3 commit 6f06632

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/ObjectSchema.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
4545
* @param {string} id - an #id
4646
**/
4747
id: id => {
48-
if (!id)
48+
if (!id) {
4949
throw new FluentSchemaError(
50-
`id should not be an empty fragment <#> or an empty string <> (e.g. #myId)`
50+
'id should not be an empty fragment <#> or an empty string <> (e.g. #myId)'
5151
)
52+
}
5253
return options.factory({ schema: { ...schema, $id: id }, ...options })
5354
},
5455
/**
@@ -277,8 +278,10 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
277278
}
278279
const target = props.def ? 'definitions' : 'properties'
279280
let attributes = props.valueOf({ isRoot: false })
281+
282+
const { $ref, $id: attributeId, required, ...restAttributes } = attributes
280283
const $id =
281-
attributes.$id ||
284+
attributeId ||
282285
(options.generateIds ? `#${target}/${name}` : undefined)
283286
if (isFluentSchema(props)) {
284287
attributes = patchIdsWithParentId({
@@ -303,8 +306,6 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
303306
? undefined
304307
: attributes.type
305308

306-
const $ref = attributes.$ref
307-
308309
// strip undefined values or empty arrays or internals
309310
attributes = Object.entries({ ...attributes, $id, type }).reduce(
310311
(memo, [key, value]) => {
@@ -323,7 +324,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
323324
...schema,
324325
[target]: [
325326
...schema[target],
326-
$ref ? { name, $ref } : Object.assign({}, { name }, attributes)
327+
$ref ? { name, $ref, ...restAttributes } : { name, ...attributes }
327328
]
328329
},
329330
...options

src/ObjectSchema.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ describe('ObjectSchema', () => {
229229
.valueOf()
230230
).toEqual({
231231
$id: id,
232-
properties: {'prop': {}},
232+
properties: { prop: {} },
233233
type: 'object'
234234
})
235235
})
@@ -1034,5 +1034,28 @@ describe('ObjectSchema', () => {
10341034
customKeyword: true
10351035
})
10361036
})
1037+
1038+
it('Carry raw properties', () => {
1039+
const schema = S.object()
1040+
.prop('test', S.ref('foo').raw({ test: true }))
1041+
.valueOf()
1042+
expect(schema).toEqual({
1043+
$schema: 'http://json-schema.org/draft-07/schema#',
1044+
type: 'object',
1045+
properties: { test: { $ref: 'foo', test: true } }
1046+
})
1047+
})
1048+
1049+
it('Carry raw properties multiple props', () => {
1050+
const schema = S.object()
1051+
.prop('a', S.string())
1052+
.prop('test', S.ref('foo').raw({ test: true }))
1053+
.valueOf()
1054+
expect(schema).toEqual({
1055+
$schema: 'http://json-schema.org/draft-07/schema#',
1056+
type: 'object',
1057+
properties: { a: { type: 'string' }, test: { $ref: 'foo', test: true } }
1058+
})
1059+
})
10371060
})
10381061
})

0 commit comments

Comments
 (0)