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

Throw on invalid schema format #162

Open
wants to merge 1 commit 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: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ var isMultipleOf = function(name, multipleOf) {
}

var compile = function(schema, cache, root, reporter, opts) {
if (Buffer.isBuffer(schema) || schema === null || (typeof schema !== 'string' && typeof schema !== 'object')) {
Copy link
Contributor

@ChALkeR ChALkeR Jul 2, 2020

Choose a reason for hiding this comment

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

No reason to be specific and check just for Buffer.
Instead, only schema === true || schema === false || schema && Object.getPrototypeOf(schema) === Object.prototype could be allowed here, like in @exodus/schemasafe.

Copy link
Contributor

@ChALkeR ChALkeR Jul 2, 2020

Choose a reason for hiding this comment

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

Also that resolves the Buffer concern from #162 (comment).

throw new Error('First argument must be either a string a normal JavaScript object')
}

var fmts = opts ? xtend(formats, opts.formats) : formats
var scope = {unique:unique, formats:fmts, isMultipleOf:isMultipleOf}
var verbose = opts ? !!opts.verbose : false;
Expand Down
22 changes: 22 additions & 0 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ var cosmic = require('./fixtures/cosmic')
var validator = require('../')
var validatorRequire = require('../require')

tape('invalid schemas', function(t) {
t.throws(function() {
validater(Buffer.from('foo'))
Copy link
Contributor

Choose a reason for hiding this comment

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

It throws because validater is undefined due to a mistype (here and in all other checks below).
This test isn't testing anything.

})
t.throws(function() {
validater(42)
})
t.throws(function() {
validater(null)
})
t.throws(function() {
validater(undefined)
})
t.throws(function() {
validater(Symbol())
})
t.throws(function() {
validater(NaN)
})
t.end()
})

tape('simple', function(t) {
var schema = {
required: true,
Expand Down