Skip to content

Commit

Permalink
#69 fix multiple extensions issue (#74)
Browse files Browse the repository at this point in the history
* #69 fix multiple extension issue

* #73 fix with better coverage

* Add Github actions
  • Loading branch information
aboutlo committed Jun 2, 2020
1 parent 1925e38 commit 5ffffb8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
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

0 comments on commit 5ffffb8

Please sign in to comment.