Skip to content

Commit

Permalink
use Array.isArray for detecting arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
awwright committed Apr 1, 2020
1 parent fabe12b commit 099061c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const coercionHook = function (instance, property, schema, options, ctx) {

// If the schema declares a type and the property fails type validation.
if (schema.type && this.attributes.type.call(this, instance, schema, options, ctx.makeChild(schema, property))) {
var types = (schema.type instanceof Array) ? schema.type : [schema.type];
var types = Array.isArray(schema.type) ? schema.type : [schema.type];
var coerced = undefined;

// Go through the declared types until we find something that we can
Expand Down
2 changes: 1 addition & 1 deletion lib/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function testSchemaNoThrow(instance, options, ctx, callback, schema){
var res = this.validateSchema(instance, schema, options, ctx);
options.throwError = throwError;

if (! res.valid && callback instanceof Function) {
if (!res.valid && callback instanceof Function) {
callback(res);
}
return res.valid;
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ exports.deepCompareStrict = function deepCompareStrict (a, b) {
if (typeof a !== typeof b) {
return false;
}
if (a instanceof Array) {
if (!(b instanceof Array)) {
if (Array.isArray(a)) {
if (!Array.isArray(b)) {
return false;
}
if (a.length !== b.length) {
Expand Down
6 changes: 3 additions & 3 deletions lib/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ module.exports.scan = function scan(base, schema){
found[ourBase.substring(0, ourBase.length-1)] = schema;
}
}
scanArray(ourBase+'/items', ((schema.items instanceof Array)?schema.items:[schema.items]));
scanArray(ourBase+'/extends', ((schema.extends instanceof Array)?schema.extends:[schema.extends]));
scanArray(ourBase+'/items', (Array.isArray(schema.items)?schema.items:[schema.items]));
scanArray(ourBase+'/extends', (Array.isArray(schema.extends)?schema.extends:[schema.extends]));
scanSchema(ourBase+'/additionalItems', schema.additionalItems);
scanObject(ourBase+'/properties', schema.properties);
scanSchema(ourBase+'/additionalProperties', schema.additionalProperties);
Expand All @@ -54,7 +54,7 @@ module.exports.scan = function scan(base, schema){
scanSchema(ourBase+'/not', schema.not);
}
function scanArray(baseuri, schemas){
if(!(schemas instanceof Array)) return;
if(!Array.isArray(schemas)) return;
for(var i=0; i<schemas.length; i++){
scanSchema(baseuri+'/'+i, schemas[i]);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Validator.prototype.addSchema = function addSchema (schema, base) {
};

Validator.prototype.addSubSchemaArray = function addSubSchemaArray(baseuri, schemas) {
if(!(schemas instanceof Array)) return;
if(!Array.isArray(schemas)) return;
for(var i=0; i<schemas.length; i++){
this.addSubSchema(baseuri, schemas[i]);
}
Expand Down Expand Up @@ -167,7 +167,7 @@ Validator.prototype.validateSchema = function validateSchema (instance, schema,
}

if (schema['extends']) {
if (schema['extends'] instanceof Array) {
if (Array.isArray(schema['extends'])) {
var schemaobj = {schema: schema, ctx: ctx};
schema['extends'].forEach(this.schemaTraverser.bind(this, schemaobj));
schema = schemaobj.schema;
Expand Down Expand Up @@ -314,7 +314,7 @@ types.any = function testAny (instance) {
};
types.object = function testObject (instance) {
// TODO: fix this - see #15
return instance && (typeof instance) === 'object' && !(instance instanceof Array) && !(instance instanceof Date);
return instance && (typeof instance === 'object') && !(Array.isArray(instance)) && !(instance instanceof Date);
};

module.exports = Validator;

0 comments on commit 099061c

Please sign in to comment.