Skip to content

Extend schema fixes #65 #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 17, 2020
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
9 changes: 5 additions & 4 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
patchIdsWithParentId,
appendRequired,
FluentSchemaError,
combineMerge,
} = require('./utils')

const initialState = {
Expand Down Expand Up @@ -264,10 +265,10 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
if (!base.isFluentSchema) {
throw new FluentSchemaError("Schema isn't FluentSchema type")
}
const state = base._getState()
const extended = merge(state, schema)

return ObjectSchema({ schema: extended, ...options })
const src = base._getState()
const extended = merge(src, schema, { arrayMerge: combineMerge })
const { valueOf, ...rest } = BaseSchema({ schema: extended, ...options })
return { valueOf }
},

/**
Expand Down
36 changes: 33 additions & 3 deletions src/ObjectSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ describe('ObjectSchema', () => {
.title('extended')
.prop('bar', S.number())
.extend(base)

expect(extended.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'extended',
Expand Down Expand Up @@ -707,7 +706,7 @@ describe('ObjectSchema', () => {
expect(extended.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
definitions: {
def1: { type: 'object', properties: { someExtended: {} } },
def1: { type: 'object', properties: { some: {}, someExtended: {} } },
def2: { type: 'object', properties: { somethingElse: {} } },
},
type: 'object',
Expand All @@ -727,6 +726,23 @@ describe('ObjectSchema', () => {
required: ['str', 'bol', 'num'],
})
})
it('extends a schema overriding the props', () => {
const base = S.object().prop('reason', S.string().title('title'))

const extended = S.object()
.prop('other')
.prop('reason', S.string().minLength(1))
.extend(base)

expect(extended.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
other: {},
reason: { title: 'title', type: 'string', minLength: 1 },
},
})
})

it('throws an error if a schema is not provided', () => {
expect(() => {
Expand All @@ -735,11 +751,25 @@ describe('ObjectSchema', () => {
new S.FluentSchemaError("Schema can't be null or undefined")
)
})
it('throws an error if a schema is not provided', () => {

it('throws an error if a schema is invalid', () => {
expect(() => {
S.object().extend('boom!')
}).toThrowError(new S.FluentSchemaError("Schema isn't FluentSchema type"))
})

it('throws an error if you append a new prop after extend', () => {
expect(() => {
const base = S.object()
S.object()
.extend(base)
.prop('foo')
}).toThrowError(
new S.FluentSchemaError(
'S.object(...).extend(...).prop is not a function'
)
)
})
})

describe('raw', () => {
Expand Down
20 changes: 19 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'

const merge = require('deepmerge')
const isFluentSchema = obj => obj && obj.isFluentSchema

const hasCombiningKeywords = attributes =>
Expand Down Expand Up @@ -36,6 +36,23 @@ const flat = array =>
}
}, {})

const combineMerge = (target, source, options) => {
const destination = target.slice()

source.forEach((item, index) => {
const prop = target.find(prop => prop.name === item.name)
if (typeof destination[index] === 'undefined') {
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
} else if (prop) {
const propIndex = target.findIndex(prop => prop.name === item.name)
destination[propIndex] = merge(prop, item, options)
} else if (target.indexOf(item) === -1) {
destination.push(item)
}
})
return destination
}

const toArray = obj =>
obj && Object.entries(obj).map(([key, value]) => ({ name: key, ...value }))

Expand Down Expand Up @@ -202,6 +219,7 @@ module.exports = {
setRaw,
setAttribute,
setComposeType,
combineMerge,
FORMATS,
TYPES,
FLUENT_SCHEMA,
Expand Down