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: determine type of combinedKeywords #237

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"scripts": {
"lint": "standard | snazzy",
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "jest --coverage",
"test:watch": "jest src/*.test.js --verbose --watch",
Expand Down
5 changes: 4 additions & 1 deletion src/BaseSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,10 @@ describe('BaseSchema', () => {
).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
properties: {
prop: { anyOf: [{ type: 'string' }, { type: 'null' }] }
prop: {
type: ['string', 'null'],
anyOf: [{ type: 'string' }, { type: 'null' }]
}
},
type: 'object'
})
Expand Down
4 changes: 3 additions & 1 deletion src/FluentSchema.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ describe('S', () => {
})

describe('compose keywords', () => {
const ajv = new Ajv()
const ajv = new Ajv({
allowUnionTypes: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because ajv will warn and spam the log output, because here type will be integer and string.

https://ajv.js.org/strict-mode.html#union-types

})
const schema = S.object()
.prop('foo', S.anyOf([S.string()]))
.prop('bar', S.not(S.anyOf([S.integer()])))
Expand Down
14 changes: 13 additions & 1 deletion src/FluentSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,24 @@ describe('S', () => {
})

describe('composition', () => {
it('anyOf', () => {
const schema = S.object()
.prop('foo', S.string().anyOf([S.string()]))
.valueOf()
expect(schema).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
properties: { foo: { type: 'string', anyOf: [{ type: 'string' }] } },
type: 'object'
})
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a similar test for oneOf?


it('anyOf', () => {
const schema = S.object()
.prop('foo', S.anyOf([S.string()]))
.valueOf()
expect(schema).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
properties: { foo: { anyOf: [{ type: 'string' }] } },
properties: { foo: { type: 'string', anyOf: [{ type: 'string' }] } },
type: 'object'
})
})
Expand All @@ -225,6 +236,7 @@ describe('S', () => {
$schema: 'http://json-schema.org/draft-07/schema#',
properties: {
multipleRestrictedTypesKey: {
type: ['string', 'number'],
oneOf: [{ type: 'string' }, { minimum: 10, type: 'number' }]
},
notTypeKey: { not: { oneOf: [{ pattern: 'js$', type: 'string' }] } }
Expand Down
5 changes: 3 additions & 2 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const {
patchIdsWithParentId,
appendRequired,
FluentSchemaError,
combineDeepmerge
combineDeepmerge,
getCombinedType
} = require('./utils')

const initialState = {
Expand Down Expand Up @@ -303,7 +304,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
}

const type = hasCombiningKeywords(attributes)
? undefined
? getCombinedType(attributes)
: attributes.type

// strip undefined values or empty arrays or internals
Expand Down
33 changes: 33 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,38 @@ const isFluentSchema = obj => obj && obj.isFluentSchema
const hasCombiningKeywords = attributes =>
attributes.allOf || attributes.anyOf || attributes.oneOf || attributes.not

const getCombinedType = (attributes) => {
const resultSet = new Set()

if (attributes.type) {
resultSet.add(attributes.type)
}

if (attributes.allOf) {
for (const item of attributes.allOf) {
resultSet.add(item.type)
}
}

if (attributes.anyOf) {
for (const item of attributes.anyOf) {
resultSet.add(item.type)
}
}

if (attributes.oneOf) {
for (const item of attributes.oneOf) {
resultSet.add(item.type)
}
}

if (resultSet.size === 1) {
return [...resultSet][0]
}

return [...resultSet]
}

class FluentSchemaError extends Error {
constructor (message) {
super(message)
Expand Down Expand Up @@ -224,6 +256,7 @@ const setComposeType = ({ prop, schemas, schema, options }) => {

module.exports = {
isFluentSchema,
getCombinedType,
hasCombiningKeywords,
FluentSchemaError,
last,
Expand Down
Loading