archetype-js
Advanced tools
Comparing version 0.1.7 to 0.1.8
{ | ||
"name": "archetype-js", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"author": "Valeri Karpov <val@boosterfuels.com>", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -6,3 +6,3 @@ 'use strict'; | ||
const _ = require('lodash'); | ||
const debug = require('debug')('monoschema:validate'); | ||
const debug = require('debug')('archetype:umarshal'); | ||
const handleCast = require('./util').handleCast; | ||
@@ -94,3 +94,4 @@ const join = require('./util').join; | ||
arr.forEach(function(value, index) { | ||
if (schema._paths[newPath].$type === Array) { | ||
if (schema._paths[newPath].$type === Array || | ||
Array.isArray(schema._paths[newPath].$type)) { | ||
let res = visitArray(value, schema, projection, join(path, index, true)); | ||
@@ -152,3 +153,4 @@ if (res.error) { | ||
if (schema._paths[newPath].$type === Array) { | ||
if (schema._paths[newPath].$type === Array || | ||
Array.isArray(schema._paths[newPath].$type)) { | ||
let res = visitArray(value, schema, projection, newPath); | ||
@@ -218,6 +220,9 @@ if (res.error) { | ||
_.each(Object.keys(schema._paths), path => { | ||
debug(`Checking validation for "${path}"`); | ||
if (shouldSkipPath(projection, path)) { | ||
debug(`Skip validation for "${path}"`); | ||
return; | ||
} | ||
if (!schema._paths[path].$validate) { | ||
debug(`No validation for "${path}"`); | ||
return true; | ||
@@ -231,9 +236,17 @@ } | ||
if (Array.isArray(val)) { | ||
_.each(val, (val, index) => { | ||
if (val.indexOf('$') === -1) { | ||
try { | ||
schema._paths[path].$validate(val, schema._paths[path], obj); | ||
} catch(_error) { | ||
error.markError(`${path}.${index}`, _error); | ||
error.markError(path, _error); | ||
} | ||
}); | ||
} else { | ||
_.each(val, (val, index) => { | ||
try { | ||
schema._paths[path].$validate(val, schema._paths[path], obj); | ||
} catch(_error) { | ||
error.markError(`${path}.${index}`, _error); | ||
} | ||
}); | ||
} | ||
} else { | ||
@@ -240,0 +253,0 @@ try { |
@@ -5,7 +5,7 @@ 'use strict'; | ||
const mongodb = require('mongodb'); | ||
const monoschema = require('../'); | ||
const archetype = require('../'); | ||
describe('schema', function() { | ||
it('compiles paths', function() { | ||
let schema = new monoschema.Schema({ | ||
let schema = new archetype.Schema({ | ||
test: Number, | ||
@@ -29,3 +29,3 @@ nested: { | ||
it('handles arrays', function() { | ||
let schema = new monoschema.Schema({ | ||
let schema = new archetype.Schema({ | ||
test: Number, | ||
@@ -42,3 +42,3 @@ arrMixed: [], | ||
'arrMixed': { $type: Array }, | ||
'arrMixed.$': { $type: monoschema.Any }, | ||
'arrMixed.$': { $type: archetype.Any }, | ||
'arrPlain': { $type: Array }, | ||
@@ -53,3 +53,3 @@ 'arrPlain.$': { $type: Number }, | ||
it('handles nested document arrays', function() { | ||
let schema = new monoschema.Schema({ | ||
let schema = new archetype.Schema({ | ||
docs: [{ _id: Number }] | ||
@@ -68,3 +68,3 @@ }); | ||
it('treats keys that start with $ as a terminus', function() { | ||
let schema = new monoschema.Schema({ | ||
let schema = new archetype.Schema({ | ||
test: { | ||
@@ -83,3 +83,3 @@ $prop: 1 | ||
it('adding paths with .path()', function() { | ||
let schema = new monoschema.Schema({ | ||
let schema = new archetype.Schema({ | ||
docs: [{ _id: Number }] | ||
@@ -96,3 +96,3 @@ }); | ||
it('ignores paths not defined in the schema', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
name: { $type: String } | ||
@@ -107,3 +107,3 @@ }); | ||
it('casts values to specified types', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
_id: { $type: mongodb.ObjectId }, | ||
@@ -130,3 +130,3 @@ name: { $type: String }, | ||
it('casts into arrays', function() { | ||
let schema = new monoschema.Schema({ | ||
let schema = new archetype.Schema({ | ||
members: [{ $type: mongodb.ObjectId }] | ||
@@ -147,3 +147,3 @@ }); | ||
it('casts deeply nested arrays', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
points: [[{ $type: Number }]] | ||
@@ -161,3 +161,3 @@ }); | ||
it('error if you cast an object to a primitive', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
name: { | ||
@@ -183,3 +183,3 @@ first: { $type: String }, | ||
it('ignores if $type not specified', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
members: { $lookUp: { ref: 'Test' } }, | ||
@@ -195,3 +195,3 @@ tags: { $type: Array } | ||
it('array of objects to primitive', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
names: [{ | ||
@@ -217,3 +217,3 @@ first: { $type: String }, | ||
it('array of objects', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
people: [{name: { $type: String, $required: true } }] | ||
@@ -230,3 +230,3 @@ }); | ||
it('required', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
name: { $type: String, $required: true } | ||
@@ -248,3 +248,3 @@ }); | ||
it('default', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
name: { $type: String, $required: true, $default: 'bacon' }, | ||
@@ -259,3 +259,3 @@ names: [{ $type: String, $required: true, $default: 'eggs' }] | ||
it('projections', function() { | ||
const schema = new monoschema.Schema({ | ||
const schema = new archetype.Schema({ | ||
name: { | ||
@@ -275,3 +275,3 @@ first: { $type: String }, | ||
it('validation', function() { | ||
const breakfastSchema = new monoschema.Schema({ | ||
const breakfastSchema = new archetype.Schema({ | ||
bacon: { | ||
@@ -292,2 +292,25 @@ $type: Number, | ||
}); | ||
it('validation with arrays', function() { | ||
const bandSchema = new archetype.Schema({ | ||
name: String, | ||
members: { | ||
$type: [String], | ||
$validate: v => { | ||
if (v.length !== 5) { | ||
throw new Error('Must have 5 members'); | ||
} | ||
} | ||
} | ||
}); | ||
assert.throws(function() { | ||
bandSchema.unmarshal({ name: "Guns N' Roses", members: ['Axl Rose'] }); | ||
}, /Must have 5 members/); | ||
bandSchema.unmarshal({ | ||
name: "Guns N' Roses", | ||
members: ['Axl Rose', 'Slash', 'Izzy', 'Duff', 'Adler'] | ||
}); | ||
}); | ||
}); |
31237
670