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
24 changes: 24 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Node.js

on: [push]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x, 13.x, 14.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run build --if-present
- run: npm test
env:
CI: true
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

10 changes: 8 additions & 2 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,14 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
}
const src = base._getState()
const extended = merge(src, schema, { arrayMerge: combineMerge })
const { valueOf, ...rest } = BaseSchema({ schema: extended, ...options })
return { valueOf }
const {
valueOf,
isFluentSchema,
FLUENT_SCHEMA,
_getState,
...rest
} = BaseSchema({ schema: extended, ...options })
return { valueOf, isFluentSchema, FLUENT_SCHEMA, _getState }
},

/**
Expand Down
36 changes: 33 additions & 3 deletions src/ObjectSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,17 @@ describe('ObjectSchema', () => {
.id('base')
.title('base')
.additionalProperties(false)
.prop('foo', S.string().minLength(5))
.prop(
'foo',
S.string()
.minLength(5)
.required(true)
)

const extended = S.object()
.id('extended')
.title('extended')
.prop('bar', S.number())
.prop('bar', S.string().required())
.extend(base)
expect(extended.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
Expand All @@ -618,12 +623,14 @@ describe('ObjectSchema', () => {
minLength: 5,
},
bar: {
type: 'number',
type: 'string',
},
},
required: ['foo', 'bar'],
type: 'object',
})
})

it('extends a nested schema', () => {
const base = S.object()
.id('base')
Expand Down Expand Up @@ -743,6 +750,29 @@ describe('ObjectSchema', () => {
},
})
})
it('extends a chain of schemas 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)

const extendedAgain = S.object()
.prop('again')
.prop('reason', S.string().minLength(2))
.extend(extended)

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

it('throws an error if a schema is not provided', () => {
expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const combineMerge = (target, source, options) => {
const destination = target.slice()

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