From 6f06632dbd2f4a0b974f2b9b9d5eaa0d40618ccf Mon Sep 17 00:00:00 2001 From: Dan Castillo Date: Wed, 26 Jul 2023 03:35:09 -0400 Subject: [PATCH] fixes: object schema carry properties added by raw to $ref (#223) * fixes: object schema carry properties added by raw to $ref fixes https://github.com/fastify/fluent-json-schema/issues/216 * Apply suggestions from code review * update to spread operator for consistency --- src/ObjectSchema.js | 13 +++++++------ src/ObjectSchema.test.js | 25 ++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/ObjectSchema.js b/src/ObjectSchema.js index dc94852..26bf978 100644 --- a/src/ObjectSchema.js +++ b/src/ObjectSchema.js @@ -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 }) }, /** @@ -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({ @@ -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]) => { @@ -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 diff --git a/src/ObjectSchema.test.js b/src/ObjectSchema.test.js index 674d860..45b227d 100644 --- a/src/ObjectSchema.test.js +++ b/src/ObjectSchema.test.js @@ -229,7 +229,7 @@ describe('ObjectSchema', () => { .valueOf() ).toEqual({ $id: id, - properties: {'prop': {}}, + properties: { prop: {} }, type: 'object' }) }) @@ -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 } } + }) + }) }) })