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 } } + }) + }) }) })