Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } }
})
})
})
})