/
index.js
218 lines (185 loc) · 4.93 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* global describe, it */
'use strict'
require('should')
var osom = require('..')
describe('schema defintion', function () {
describe('simple rule', function () {
it('empty value', function () {
[{age: Number}, {age: { type: Number }}].forEach(function (rule) {
osom(rule)().should.be.eql({})
})
})
it('providing value', function () {
[{age: Number}, {age: { type: Number }}].forEach(function (rule) {
osom(rule)({age: '23'}).should.be.eql({age: 23})
})
})
})
it('empty values', function () {
[Number, String, Function, Boolean].forEach(function (type) {
var schema = {
age: {
type: type
}
}
var validator = osom(schema)
;[null, {}].forEach(function (data) {
validator(data).should.be.eql({})
})
})
})
describe('support default values', function () {
it('based in a value', function () {
var schema = {
age: {
type: Number, default: 23
}
}
var validator = osom(schema)
validator().should.be.eql({age: 23})
})
it('based in a fn', function () {
var schema = {
age: {
type: Number, default: function () { return 23 }
}
}
var validator = osom(schema)
validator().should.be.eql({age: 23})
})
})
it('support transforms', function () {
function trim (str) {
return str.trim()
}
var schema = {
age: {
type: String,
transform: [trim]
}
}
var validator = osom(schema)
validator({age: ' 23 '}).should.be.eql({age: '23'})
})
describe('support required values', function () {
it('throw an error under no values', function () {
var schema = {
age: {
type: String,
required: true
}
}
var validator = osom(schema)
var errMessage = "Expected a {string} for 'age'."
;(function () { validator() }).should.throw(errMessage)
})
it('custom error message', function () {
var schema = {
age: {
type: String,
required: 'your message here'
}
}
var validator = osom(schema)
var errMessage = 'your message here'
;(function () { validator() }).should.throw(errMessage)
})
})
describe('support casting', function () {
it('enable (by default)', function () {
var schema = { age: Array }
var validator = osom(schema)
validator({age: '23'}).should.be.eql({age: ['23']})
})
it('disable explicit', function () {
var schema = {
age: {
type: String,
casting: false
}
}
var errMessage = "Expected a {string} for 'age'."
var validator = osom(schema)
;[{age: 23}].forEach(function (obj) {
;(function () { validator(obj) }).should.throw(errMessage)
})
})
it('disable explicit works with nill values', function () {
[Number, String, Function, Boolean].forEach(function (type) {
var schema = {
age: {
type: type,
casting: false
}
}
var validator = osom(schema)
;[null, {}, {age: null}].forEach(function (data) {
validator(data).should.be.eql({})
})
})
})
})
describe('support validation', function () {
it('based in a function', function () {
var schema = {
age: {
type: String,
validate: function (v) {
return v === '23'
}
}
}
var validator = osom(schema)
var errMessage = "Fail '25' validation for 'age'."
;(function () { validator({age: 25}) }).should.throw(errMessage)
})
it('based in a object key', function () {
var schema = {
age: {
type: String,
validate: {
validator: function (v) {
return v === '23'
}
}
}
}
var validator = osom(schema)
var errMessage = "Fail '25' validation for 'age'."
;(function () { validator({age: 25}) }).should.throw(errMessage)
})
it('custom error message', function () {
var schema = {
age: {
type: String,
validate: {
validator: function (v) {
return v === '23'
},
message: 'expected a millenial value instead of {VALUE}!'
}
}
}
var validator = osom(schema)
var errMessage = 'expected a millenial value instead of 25!'
;(function () { validator({age: 25}) }).should.throw(errMessage)
})
})
})
describe('behavior', function () {
it('custom global values', function () {
function trim (str) {
return str.trim()
}
var schema = {
age: {
type: String
}
}
var globalFields = {
transform: [trim]
}
var validator = osom(schema, globalFields)
validator({age: ' 23 '}).should.be.eql({age: '23'})
})
})