-
Notifications
You must be signed in to change notification settings - Fork 46
/
resolve.js
129 lines (116 loc) · 3 KB
/
resolve.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
var Person = {
type: 'object',
fields: {
name: {
type: 'string',
required: true
}
}
};
var Group = {
type: 'object',
fields: {
name: {
type: 'string',
required: true
},
brand: {
type: 'string'
}
}
};
function resolve() {
switch(this.type) {
case 'Person':
return Person;
case 'Group':
return Group;
}
throw new Error('Unknown performer type');
}
var descriptor = {
type: 'object',
fields: {
performer: {
type: 'object',
resolve: resolve
}
}
}
var mixed = {
type: 'object',
fields: {
performer: {
type: 'array',
values: {
type: 'object',
resolve: resolve
}
}
}
}
it("should use resolve function error on required (Person)", function(done) {
var schema = new Schema(descriptor)
, source = {performer: {type: 'Person'}};
schema.validate(source, function(err, res){
expect(res.errors.length).to.eql(1);
expect(res.fields.name.length).to.eql(1);
expect(res.errors[0].message).to.eql('name is required');
done();
});
});
it("should use resolve function (Person type)", function(done) {
var schema = new Schema(descriptor)
, source = {performer: {type: 'Person', name: 'joe'}};
schema.validate(source, function(err, res){
expect(err).to.eql(null);
expect(res).to.eql(null);
done();
});
});
it("should use resolve function error on required (Group)", function(done) {
var schema = new Schema(descriptor)
, source = {performer: {type: 'Group'}};
schema.validate(source, function(err, res){
expect(res.errors.length).to.eql(1);
expect(res.fields.name.length).to.eql(1);
expect(res.errors[0].message).to.eql('name is required');
done();
});
});
it("should use resolve function (Group type)", function(done) {
var schema = new Schema(descriptor)
, source = {performer: {type: 'Group', name: 'john'}};
schema.validate(source, function(err, res){
expect(err).to.eql(null);
expect(res).to.eql(null);
done();
});
});
it("should throw error on unknown type", function(done) {
var schema = new Schema(descriptor)
, source = {performer: {type: 'Unknown', name: 'joe'}};
function fn() {
schema.validate(source, function(/*err, res*/){});
}
expect(fn).throws(/Unknown performer type/i);
done();
});
it("should use resolve function (mixed types)", function(done) {
var schema = new Schema(mixed)
, source = {
performer: [
{type: 'Person', name: 'joe'},
{type: 'Group', name: 'john'}
]
};
schema.validate(source, function(err, res){
expect(err).to.eql(null);
expect(res).to.eql(null);
done();
});
});
});