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: handle custom formats with null values #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ var compile = function(schema, cache, root, reporter, opts) {
}

if (node.format && fmts[node.format]) {
if (type !== 'string' && formats[node.format]) validate('if (%s) {', types.string(name))
if (type !== 'string' && fmts[node.format]) validate('if (%s) {', types.string(name))
Copy link
Collaborator

Choose a reason for hiding this comment

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

fmts[node.format] will always be truthy since it's checked in the outer if

Copy link
Author

Choose a reason for hiding this comment

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

that's good observation, but does not affect the fix. the only difference between formats and fmts is that fmts has user supplied custom formats and built-ins.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I meant that you can remove that part alltogether, since it will always be true:

if (type !== 'string') validate('if (%s) {', types.string(name))

var n = gensym('format')
scope[n] = fmts[node.format]

if (typeof scope[n] === 'function') validate('if (!%s(%s)) {', n, name)
else validate('if (!%s.test(%s)) {', n, name)
error('must be '+node.format+' format')
validate('}')
if (type !== 'string' && formats[node.format]) validate('}')
if (type !== 'string' && fmts[node.format]) validate('}')
}

if (Array.isArray(node.required)) {
Expand Down
19 changes: 19 additions & 0 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,25 @@ tape('custom format', function(t) {
t.end()
})

tape('custom format string or null', function (t) {
var validate = validator({
type: 'object',
properties: {
foo: {
type: ['string', 'null'],
format: 'as'
}
}
}, {formats: {as:/^a+$/}})

t.notOk(validate({foo:''}), 'not as')
t.notOk(validate({foo:'bar'}), 'not as')
t.notOk(validate({foo:123}), 'not as if number')
t.ok(validate({foo:'a'}), 'as')
t.ok(validate({foo:null}), 'as')
t.end()
})

tape('custom format function', function(t) {
var validate = validator({
type: 'object',
Expand Down