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: throw on root-level required #134

Merged
merged 2 commits into from
Jun 18, 2021
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
13 changes: 10 additions & 3 deletions src/BaseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ const BaseSchema = (
: currentProp
? [...schema.required, currentProp.name]
: [REQUIRED]

if(!isUniq(required)){
throw new FluentSchemaError("'required' has repeated keys, check your calls to require()")
throw new FluentSchemaError("'required' has repeated keys, check your calls to .required()")
}

return options.factory({
Expand Down Expand Up @@ -406,10 +406,17 @@ const BaseSchema = (
/**
* It returns all the schema values
*
* @param {Object} [options] - Options
* @param {boolean} [options.isRoot = true] - Is a root level schema
* @returns {object}
*/
valueOf: () => {
valueOf: ({ isRoot } = { isRoot: true }) => {
const { properties, definitions, required, $schema, ...rest } = schema

if (isRoot && required && !required.every((v) => typeof v === 'string')) {
throw new FluentSchemaError("'required' has called on root-level schema, check your calls to .required()")
}

return Object.assign(
$schema ? { $schema } : {},
Object.keys(definitions || []).length > 0
Expand Down
14 changes: 12 additions & 2 deletions src/BaseSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('BaseSchema', () => {
.prop("A", S.string()).required().required()
}).toThrowError(
new S.FluentSchemaError(
"'required' has repeated keys, check your calls to require()"
"'required' has repeated keys, check your calls to .required()"
)
)
})
Expand All @@ -172,12 +172,22 @@ describe('BaseSchema', () => {
.prop("A", S.string().required())
}).toThrowError(
new S.FluentSchemaError(
"'required' has repeated keys, check your calls to require()"
"'required' has repeated keys, check your calls to .required()"
)
)
})
})

it('root-level required', () => {
expect(() => {
return S.object().required().valueOf()
}).toThrowError(
new S.FluentSchemaError(
"'required' has called on root-level schema, check your calls to .required()"
)
)
})

describe('array', () => {
it('simple', () => {
const required = ['foo', 'bar']
Expand Down
12 changes: 6 additions & 6 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
])
}
if (isFluentSchema(value)) {
const { $schema, ...rest } = value.valueOf()
const { $schema, ...rest } = value.valueOf({ isRoot: false })
return setAttribute({ schema, ...options }, [
'additionalProperties',
{ ...rest },
Expand Down Expand Up @@ -127,7 +127,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
)
return {
...memo,
[pattern]: omit(schema.valueOf(), ['$schema']),
[pattern]: omit(schema.valueOf({ isRoot: false }), ['$schema']),
}
}, {})
return setAttribute({ schema, ...options }, [
Expand Down Expand Up @@ -158,7 +158,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
...memo,
[prop]: Array.isArray(schema)
? schema
: omit(schema.valueOf(), ['$schema', 'type', 'definitions']),
: omit(schema.valueOf({ isRoot: false }), ['$schema', 'type', 'definitions']),
}
}, {})
return setAttribute({ schema, ...options }, [
Expand All @@ -182,7 +182,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
throw new FluentSchemaError("'propertyNames' must be a S")
return setAttribute({ schema, ...options }, [
'propertyNames',
omit(value.valueOf(), ['$schema']),
omit(value.valueOf({ isRoot: false }), ['$schema']),
'object',
])
},
Expand All @@ -204,7 +204,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
)}'. Pass a FluentSchema object`
)
const target = props.def ? 'definitions' : 'properties'
let attributes = props.valueOf()
let attributes = props.valueOf({ isRoot: false })
const $id =
attributes.$id ||
(options.generateIds ? `#${target}/${name}` : undefined)
Expand Down Expand Up @@ -310,7 +310,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
// TODO LS Is a definition a proper schema?
definition: (name, props = {}) =>
ObjectSchema({ schema, ...options }).prop(name, {
...props.valueOf(),
...props.valueOf({ isRoot: false }),
def: true,
}),
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const appendRequired = ({

const patchedRequired = [...schema.required, ...schemaRequired];
if(!isUniq(patchedRequired)){
throw new FluentSchemaError("'required' has repeated keys, check your calls to require()")
throw new FluentSchemaError("'required' has repeated keys, check your calls to .required()")
}

const schemaPatched = {
Expand Down Expand Up @@ -205,7 +205,7 @@ const setComposeType = ({ prop, schemas, schema, options }) => {
}

const values = schemas.map(schema => {
const { $schema, ...props } = schema.valueOf()
const { $schema, ...props } = schema.valueOf({ isRoot: false })
return props
})

Expand Down