| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,133 @@ |
| +'use strict'; |
| + |
| +const start = require('./common'); |
| + |
| +const assert = require('assert'); |
| + |
| +const mongoose = start.mongoose; |
| +const Schema = mongoose.Schema; |
| + |
| +describe('document validateAllPaths option', function() { |
| + let modelCounter = 0; |
| + function makeModel(definition, schemaOptions) { |
| + const schema = new Schema(definition, schemaOptions); |
| + return mongoose.model('validateAllPaths_' + (++modelCounter), schema); |
| + } |
| + |
| + describe('with schema-level validateModifiedOnly', function() { |
| + let Test; |
| + function makeInvalidDoc() { |
| + return Test.hydrate({ name: '', age: -1, tags: ['z'], okField: 'fine' }); |
| + } |
| + |
| + beforeEach(function() { |
| + Test = makeModel({ |
| + name: { type: String, required: true }, |
| + age: { type: Number, min: [0, 'too small'] }, |
| + tags: [{ type: String, enum: ['a', 'b'] }], |
| + okField: { type: String } |
| + }, { validateModifiedOnly: true }); |
| + }); |
| + |
| + it('validateSync does not report unmodified paths by default', function() { |
| + const doc = makeInvalidDoc(); |
| + assert.strictEqual(doc.modifiedPaths().length, 0); |
| + assert.strictEqual(doc.validateSync(), undefined); |
| + }); |
| + |
| + it('validateSync({ validateAllPaths: true }) validates every path', function() { |
| + const doc = makeInvalidDoc(); |
| + const err = doc.validateSync({ validateAllPaths: true }); |
| + assert.ok(err); |
| + assert.equal(err.name, 'ValidationError'); |
| + assert.ok(err.errors['name']); |
| + assert.ok(err.errors['age']); |
| + assert.ok(err.errors['tags.0']); |
| + assert.ok(!err.errors['okField']); |
| + }); |
| + |
| + it('validateSync({ validateAllPaths: true }) returns undefined when all paths are valid', function() { |
| + const doc = Test.hydrate({ name: 'valid', age: 5, tags: ['a'], okField: 'fine' }); |
| + assert.strictEqual(doc.validateSync({ validateAllPaths: true }), undefined); |
| + }); |
| + |
| + it('validate() does not report unmodified paths by default', async function() { |
| + const doc = makeInvalidDoc(); |
| + await doc.validate(); |
| + }); |
| + |
| + it('validate({ validateAllPaths: true }) rejects with every failing path', async function() { |
| + const doc = makeInvalidDoc(); |
| + const err = await doc.validate({ validateAllPaths: true }).then(() => null, e => e); |
| + assert.ok(err); |
| + assert.equal(err.name, 'ValidationError'); |
| + assert.ok(err.errors['name']); |
| + assert.ok(err.errors['age']); |
| + assert.ok(err.errors['tags.0']); |
| + assert.ok(!err.errors['okField']); |
| + }); |
| + |
| + it('validate({ validateAllPaths: true }) resolves when all paths are valid', async function() { |
| + const doc = Test.hydrate({ name: 'valid', age: 5, tags: ['a'], okField: 'fine' }); |
| + await doc.validate({ validateAllPaths: true }); |
| + }); |
| + }); |
| + |
| + describe('error contracts', function() { |
| + let Test; |
| + beforeEach(function() { |
| + Test = makeModel({ |
| + name: { type: String, required: true }, |
| + age: { type: Number } |
| + }); |
| + }); |
| + |
| + it('validateSync throws when combining validateAllPaths and pathsToSkip', function() { |
| + const doc = Test.hydrate({ name: 'x', age: 1 }); |
| + assert.throws( |
| + () => doc.validateSync({ validateAllPaths: true, pathsToSkip: 'name' }), |
| + err => err instanceof TypeError && |
| + err.message === 'Cannot set both `validateAllPaths` and `pathsToSkip`' |
| + ); |
| + }); |
| + |
| + it('validateSync throws when combining validateAllPaths and pathsToValidate', function() { |
| + const doc = Test.hydrate({ name: 'x', age: 1 }); |
| + assert.throws( |
| + () => doc.validateSync(['name'], { validateAllPaths: true }), |
| + err => err instanceof TypeError && |
| + err.message === 'Cannot set both `validateAllPaths` and `pathsToValidate`' |
| + ); |
| + }); |
| + |
| + it('validateSync throws when combining validateAllPaths and validateModifiedOnly', function() { |
| + const doc = Test.hydrate({ name: 'x', age: 1 }); |
| + assert.throws( |
| + () => doc.validateSync({ validateAllPaths: true, validateModifiedOnly: true }), |
| + err => err instanceof TypeError && |
| + err.message === 'Cannot set both `validateAllPaths` and `validateModifiedOnly`' |
| + ); |
| + }); |
| + |
| + it('validate rejects when combining validateAllPaths and pathsToSkip', async function() { |
| + const doc = Test.hydrate({ name: 'x', age: 1 }); |
| + const err = await doc.validate({ validateAllPaths: true, pathsToSkip: 'name' }).then(() => null, e => e); |
| + assert.ok(err instanceof TypeError); |
| + assert.equal(err.message, 'Cannot set both `validateAllPaths` and `pathsToSkip`'); |
| + }); |
| + |
| + it('validate rejects when combining validateAllPaths and pathsToValidate', async function() { |
| + const doc = Test.hydrate({ name: 'x', age: 1 }); |
| + const err = await doc.validate(['name'], { validateAllPaths: true }).then(() => null, e => e); |
| + assert.ok(err instanceof TypeError); |
| + assert.equal(err.message, 'Cannot set both `validateAllPaths` and `pathsToValidate`'); |
| + }); |
| + |
| + it('validate rejects when combining validateAllPaths and validateModifiedOnly', async function() { |
| + const doc = Test.hydrate({ name: 'x', age: 1 }); |
| + const err = await doc.validate({ validateAllPaths: true, validateModifiedOnly: true }).then(() => null, e => e); |
| + assert.ok(err instanceof TypeError); |
| + assert.equal(err.message, 'Cannot set both `validateAllPaths` and `validateModifiedOnly`'); |
| + }); |
| + }); |
| +}); |
| |
| new file mode 100755 |
| |
| |
| @@ -0,0 +1,22 @@ |
| +#!/usr/bin/env bash |
| + |
| + |
| + |
| + |
| +set -euo pipefail |
| +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| +cd /testbed |
| + |
| + |
| + |
| + |
| + |
| + |
| + |
| +export MONGOMS_VERSION=8.0.0 |
| + |
| + |
| +npx mocha --exit ./test/document.test.js |
| + |
| + |
| +npx mocha --exit ./test/document.validateAllPaths.test.js |
|
|