deno.land / x / mongoose@6.7.5 / test / errors.validation.test.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
/** * Module dependencies. */
'use strict';
const start = require('./common');
const ValidationError = require('../lib/error/validation');const assert = require('assert');
const mongoose = start.mongoose;const Schema = mongoose.Schema;const SchemaType = mongoose.SchemaType;const ValidatorError = SchemaType.ValidatorError;
describe('ValidationError', function() { describe('#infiniteRecursion', function() { it('does not cause RangeError (gh-1834)', async function() { const SubSchema = new Schema({ name: { type: String, required: true }, contents: [ new Schema( { key: { type: String, required: true }, value: { type: String, required: true } }, { _id: false } ) ] });
const M = mongoose.model('SubSchema', SubSchema);
const model = new M({ name: 'Model', contents: [ { key: 'foo' } ] });
const err = await model.validate().then(() => null, err => err);
assert.doesNotThrow(function() { JSON.stringify(err); }); }); });
describe('#minDate', function() { it('causes a validation error', async function() { const MinSchema = new Schema({ appointmentDate: { type: Date, min: Date.now } });
const M = mongoose.model('MinSchema', MinSchema);
const model = new M({ appointmentDate: new Date(Date.now().valueOf() - 10000) });
// should fail validation const err = await model.validate().then(() => null, err => err);
assert.notEqual(err, null, 'min Date validation failed.'); assert.ok(err.message.startsWith('MinSchema validation failed')); model.appointmentDate = new Date(Date.now().valueOf() + 10000);
// should pass validation await model.validate(); }); });
describe('#maxDate', function() { it('causes a validation error', async function() { const MaxSchema = new Schema({ birthdate: { type: Date, max: Date.now } });
const M = mongoose.model('MaxSchema', MaxSchema);
const model = new M({ birthdate: new Date(Date.now().valueOf() + 2000) });
// should fail validation const err = await model.validate().then(() => null, err => err);
assert.notEqual(err, null, 'max Date validation failed'); assert.ok(err.message.startsWith('MaxSchema validation failed')); model.birthdate = Date.now();
// should pass validation await model.validate(); }); });
describe('#minLength', function() { it('causes a validation error', async function() { const AddressSchema = new Schema({ postalCode: { type: String, minlength: 5 }, zipCode: { type: String, minLength: 5 } });
const Address = mongoose.model('MinLengthAddress', AddressSchema);
const model = new Address({ postalCode: '9512', zipCode: '9512' });
// should fail validation const err = await model.validate().then(() => null, err => err);
assert.notEqual(err, null, 'String minLength validation failed.'); assert.ok(err.message.startsWith('MinLengthAddress validation failed')); model.postalCode = '95125'; model.zipCode = '95125';
// should pass validation await model.validate(); });
it('with correct error message (gh-4207)', async function() { const old = mongoose.Error.messages; mongoose.Error.messages = { String: { minlength: 'woops!' } };
const AddressSchema = new Schema({ postalCode: { type: String, minlength: 5 }, zipCode: { type: String, minLength: 5 } });
const Address = mongoose.model('gh4207', AddressSchema);
const model = new Address({ postalCode: '9512', zipCode: '9512' });
// should fail validation const err = await model.validate().then(() => null, err => err);
assert.equal(err.errors['postalCode'].message, 'woops!'); assert.ok(err.message.startsWith('gh4207 validation failed')); mongoose.Error.messages = old; }); });
describe('#maxLength', function() { it('causes a validation error', async function() { const AddressSchema = new Schema({ postalCode: { type: String, maxlength: 10 }, zipCode: { type: String, maxLength: 10 } });
const Address = mongoose.model('MaxLengthAddress', AddressSchema);
const model = new Address({ postalCode: '95125012345', zipCode: '95125012345' });
// should fail validation const err = await model.validate().then(() => null, err => err);
assert.notEqual(err, null, 'String maxLength validation failed.'); assert.ok(err.message.startsWith('MaxLengthAddress validation failed')); model.postalCode = '95125'; model.zipCode = '95125';
// should pass validation await model.validate(); }); });
describe('#toString', function() { it('does not cause RangeError (gh-1296)', async function() { const ASchema = new Schema({ key: { type: String, required: true }, value: { type: String, required: true } });
const BSchema = new Schema({ contents: [ASchema] });
const M = mongoose.model('A', BSchema); const m = new M(); m.contents.push({ key: 'asdf' }); const err = await m.validate().then(() => null, err => err);
assert.doesNotThrow(function() { String(err); }); }); });
describe('formatMessage', function() { it('replaces properties in a message', function() { const props = { base: 'eggs', topping: 'bacon' }; const message = 'I had {BASE} and {TOPPING} for breakfast';
const result = ValidatorError.prototype.formatMessage(message, props); assert.equal(result, 'I had eggs and bacon for breakfast'); }); });
it('JSON.stringify() with message (gh-5309) (gh-9296)', function() { model.modelName = 'TestClass'; const err = new ValidationError(new model());
err.addError('test', new ValidatorError({ message: 'Fail' }));
const obj = JSON.parse(JSON.stringify(err)); assert.ok(obj.message.indexOf('TestClass validation failed') !== -1, obj.message); assert.ok(obj.message.indexOf('test: Fail') !== -1, obj.message); assert.ok(obj.errors['test'].message);
function model() {} });
it('default error message', function() { const err = new ValidationError();
assert.equal(err.message, 'Validation failed'); });
describe('when user code defines a r/o Error#toJSON', function() { it('should not fail', function(done) { this.timeout(10000);
const err = []; const child = require('child_process') .fork('./test/isolated/project-has-error.toJSON.js', ['--no-warnings'], { silent: true });
child.stderr.on('data', function(buf) { err.push(buf); }); child.on('exit', function(code) { const stderr = err.filter(line => !line.includes('Warning: ')).join(''); assert.equal(stderr, ''); assert.equal(code, 0); done(); }); }); }); it('should have error name in Cast error gh-10166', async function() { const zetaSchema = new Schema({ text: { type: String, required: [true, 'Text is required'] }, number: { type: Number, required: [true, 'Number is required'] } }); const Zeta = mongoose.model('Zeta', zetaSchema); const entry = new Zeta({ text: false, number: 'fsfsf' });
const error = await entry.validate().then(() => null, err => err);
assert.ok(JSON.parse(JSON.stringify(error.errors.number.message))); assert.ok(JSON.parse(JSON.stringify(error.errors.number.name))); });});
Version Info