Skip to content
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

fix: ObjectSchema.extend returns a schema instance #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 1 addition & 8 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
}
const src = base._getState()
const extended = combineDeepmerge(src, schema)
const {
valueOf,
isFluentSchema,
FLUENT_SCHEMA,
_getState,
extend
} = ObjectSchema({ schema: extended, ...options })
return { valueOf, isFluentSchema, FLUENT_SCHEMA, _getState, extend }
return ObjectSchema({ schema: extended, ...options })
},

/**
Expand Down
32 changes: 19 additions & 13 deletions src/ObjectSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,25 @@ describe('ObjectSchema', () => {
})
})

it('extends schemas in a chain supporting without after extend', () => {
const base = S.object()
.prop('foo')

const extended = S.object()
.prop('bar')
.prop('baz')
.extend(base)
.without(['foo'])
assert.deepStrictEqual(extended.valueOf(), {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
bar: {},
baz: {}
}
})
})

it('throws an error if a schema is not provided', () => {
assert.throws(
() => S.object().extend(),
Expand All @@ -861,19 +880,6 @@ describe('ObjectSchema', () => {
err.message === "Schema isn't FluentSchema type"
)
})

it('throws an error if you append a new prop after extend', () => {
assert.throws(
() => {
const base = S.object()
S.object().extend(base).prop('foo')
},
(err) =>
// err instanceof S.FluentSchemaError &&
err instanceof TypeError &&
err.message === 'S.object(...).extend(...).prop is not a function'
)
})
})

describe('only', () => {
Expand Down