-
Notifications
You must be signed in to change notification settings - Fork 906
/
string.js
170 lines (134 loc) · 4.43 KB
/
string.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
'use strict';
import Promise from 'promise/src/es6-extensions'
import { string, number, object, ref } from '../src';
describe('String types', function(){
describe('casting', ()=> {
let schema = string();
TestHelpers.castAll(schema, {
valid: [
[5, '5'],
['3', '3'],
//[new String('foo'), 'foo'],
['', ''],
[true, 'true'],
[false, 'false'],
[0, '0'],
[null, null, schema.nullable()]
],
invalid: [
null,
]
})
describe('ensure', () => {
let schema = string().ensure();
TestHelpers.castAll(
schema,
{
valid: [
[5, '5'],
['3', '3'],
[null, ''],
[undefined, ''],
[null, '', schema.default('foo')],
[undefined, 'foo', schema.default('foo')],
]
}
)
})
it('should trim', () => {
schema.trim().cast(' 3 ').should.equal('3')
})
it('should transform to lowercase', () => {
schema.lowercase()
.cast('HellO JohN')
.should.equal('hello john')
})
it('should transform to lowercase', () => {
schema.uppercase()
.cast('HellO JohN')
.should.equal('HELLO JOHN')
})
it('should handle nulls', () => {
expect(schema.nullable()
.trim()
.lowercase()
.uppercase()
.cast(null)).to.equal(null)
})
})
it('should handle DEFAULT', function(){
var inst = string()
inst.default('my_value').required().default().should.equal('my_value')
})
it('should type check', function(){
var inst = string()
inst.isType('5').should.equal(true)
inst.isType(new String('5')).should.equal(true)
inst.isType(false).should.equal(false)
inst.isType(null).should.equal(false)
inst.nullable(false).isType(null).should.equal(false)
})
it('should VALIDATE correctly', function(){
var inst = string().required().min(4).strict()
return Promise.all([
string().strict().isValid(null).should.eventually.equal(false),
string().strict().nullable(true).isValid(null).should.eventually.equal(true),
inst.isValid('hello').should.eventually.equal(true),
inst.isValid('hel').should.eventually.equal(false),
inst.validate('').should.be.rejected.then(function(err) {
err.errors.length.should.equal(1)
})
])
})
it('should check MATCHES correctly', function(){
var v = string().matches(/(hi|bye)/);
return Promise.all([
v.isValid('hi').should.eventually.equal(true),
v.isValid('nope').should.eventually.equal(false),
v.isValid('bye').should.eventually.equal(true)
])
})
it('should check MIN correctly', function(){
var v = string().min(5);
var obj = object({
len: number(),
name: string().min(ref('len'))
})
return Promise.all([
v.isValid('hiiofff').should.eventually.equal(true),
v.isValid('big').should.eventually.equal(false),
v.isValid('noffasfasfasf saf').should.eventually.equal(true),
v.isValid(null).should.eventually.equal(false), // null -> ''
v.nullable().isValid(null).should.eventually.equal(true), // null -> null
obj.isValid({ len: 10, name: 'john' }).should.eventually.equal(false)
])
})
it('should check MAX correctly', function(){
var v = string().max(5);
var obj = object({
len: number(),
name: string().max(ref('len'))
})
return Promise.all([
v.isValid('adgf').should.eventually.equal(true),
v.isValid('bigdfdsfsdf').should.eventually.equal(false),
v.isValid('no').should.eventually.equal(true),
v.isValid(null).should.eventually.equal(false),
v.nullable().isValid(null).should.eventually.equal(true),
obj.isValid({ len: 3, name: 'john' }).should.eventually.equal(false)
])
})
it('should validate transforms', function(){
return Promise.all([
string().trim().isValid(' 3 ').should.eventually.equal(true),
string().lowercase().isValid('HellO JohN').should.eventually.equal(true),
string().uppercase().isValid('HellO JohN').should.eventually.equal(true),
string().trim().isValid(' 3 ', { strict: true })
.should.eventually.equal(false),
string().lowercase().isValid('HellO JohN', { strict: true })
.should.eventually.equal(false),
string().uppercase().isValid('HellO JohN', { strict: true })
.should.eventually.equal(false)
])
})
})