deno.land / x / mongoose@6.7.5 / test / docs / custom-casting.test.js

custom-casting.test.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
const assert = require('assert');const Mongoose = require('../../lib');
const mongoose = new Mongoose.Mongoose();
describe('custom casting', function() { let originalCast;
beforeEach(function() { originalCast = mongoose.Number.cast(); });
afterEach(function() { mongoose.deleteModel('Test'); mongoose.Number.cast(originalCast); });
it('casting error', function() { const schema = new mongoose.Schema({ age: Number }); const Model = mongoose.model('Test', schema);
const doc = new Model({ age: '二' }); const err = doc.validateSync(); // "Cast to Number failed for value "二" at path "age"" err.message; // acquit:ignore:start assert.ok(err.message.indexOf('Cast to Number failed for value "二" (type string) at path "age"') !== -1, err.message); // acquit:ignore:end });
it('casting override', function() { // Calling `cast()` on a class that inherits from `SchemaType` returns the // current casting function. const originalCast = mongoose.Number.cast();
// Calling `cast()` with a function sets the current function used to // cast a given schema type, in this cast Numbers. mongoose.Number.cast(v => { if (v === '二') { return 2; } return originalCast(v); });
const schema = new mongoose.Schema({ age: Number });
const Model = mongoose.model('Test', schema);
const doc = new Model({ age: '二' }); const err = doc.validateSync(); err; // null doc.age; // 2 // acquit:ignore:start assert.ifError(err); assert.equal(doc.age, 2); // acquit:ignore:end });});
mongoose

Version Info

Tagged at
a year ago