Skip to content

Commit

Permalink
fix: add default value for type
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Apr 13, 2019
1 parent 54ef7ab commit 1e40240
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ async function jsonschema() {
} else {
schema.properties.type.enum = [schema.title]
}
schema.properties.type.default = schema.title
}

// Remove unnecessary processing keywords
Expand Down
40 changes: 40 additions & 0 deletions tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,46 @@ describe('mutate', () => {
})
})

it('will correct nested nodes including adding type', () => {
const article = mutate({
authors: [{
givenNames: 'Joe'
}, {
givenNames: ['Jane', 'Jill'],
familyNames: 'Jones'
}]
}, 'Article')

expect(article.authors[0].type).toEqual('Person')
expect(cast(article.authors[0], 'Person').givenNames).toEqual(['Joe'])
expect(article.authors[1].type).toEqual('Person')
expect(cast(article.authors[1], 'Person').familyNames).toEqual(['Jones'])
})

it('currently has a bug with arrays using anyOf', () => {
const article = mutate({
authors: [{
givenNames: ['Joe']
}, {
// Even though we explicitly state that this is an
// `Organization`, `legalName` gets dropped because
// Ajv sees it as an additional property for `Person`
// This is a bug in Ajv.
type: 'Organization',
name: 'Example Uni',
legalName: 'Example University Inc.'
}]
}, 'Article')

expect(article.authors[0].type).toEqual('Person')

expect(article.authors[1].type).toEqual('Organization')
expect(article.authors[1].name).toEqual('Example Uni')
// @ts-ignore
expect(article.authors[1].legalName).toBeUndefined()
expect(cast(article.authors[1], 'Organization').legalName).toBeUndefined()
})

it('throws an error if unable to coerce data, or data is otherwise invalid', () => {
expect(() =>
mutate(
Expand Down

0 comments on commit 1e40240

Please sign in to comment.