This repository has been archived by the owner on Nov 15, 2020. It is now read-only.
/
complex.js
125 lines (112 loc) · 3.49 KB
/
complex.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
describe("complex schemas", function()
{
/*jshint expr:true*/
var schemajs = (typeof window === 'undefined') ? require('../schema') : window.schema;
var expect = (typeof window === 'undefined') ? require('chai').expect : window.chai.expect;
it("arrays", function()
{
var schema = schemajs.create(
{
input:
{
type:'array',
schema:
{
type: 'number'
}
}
});
var schema2 = schemajs.create(
{
input:
{
type:'array',
schema:
{
type: 'number'
},
allownull: true
}
});
var input1 = schema.validate({input: 'username'});
var input2 = schema.validate({input: [112390123]});
var input3 = schema.validate({input: [112390123, 'username']});
var input4 = schema.validate({});
var input5 = schema.validate({input: null});
var input6 = schema2.validate({input: null});
expect(!input1.valid).to.be.ok;
expect(input2.valid).to.be.ok;
expect(!input3.valid).to.be.ok;
expect(input4.valid).to.be.ok;
expect(!input5.valid).to.be.ok;
expect(input6.valid).to.be.ok;
});
it("arrays and objects", function()
{
var schema = schemajs.create(
{
input:
{
type:'array',
schema:
{
type: 'object',
schema: {
label:{type:"string+"},
num:{type:"number"}
}
}
}
});
var input1 = schema.validate({input: 'laksjdf'});
var input2 = schema.validate({input: [{label:"hi", num:5}, {label:"bye", num:0}]});
var input3 = schema.validate({input: [{label:"hi", num:5}, {label:0, num:0}]});
var input4 = schema.validate({input: [{label:"hi", num:5}, "oops"]});
var input5 = schema.validate({});
expect(!input1.valid).to.be.ok;
expect(input2.valid).to.be.ok;
expect(!input3.valid).to.be.ok;
expect(!input4.valid).to.be.ok;
expect(input5.valid).to.be.ok;
});
it("testing complex object type", function()
{
var schema = schemajs.create(
{
input:
{
type:'object',
schema:
{
name: { type: "string", properties: { max: 255 }, required: true},
email: { type: "email", error: "email is not a valid email address"}
}
}
});
var schema2 = schemajs.create(
{
input:
{
type:'object',
schema:
{
name: { type: "string", properties: { max: 255 }, required: true},
email: { type: "email", error: "email is not a valid email address"}
},
allownull: true
}
});
var input1 = schema.validate({input: 'username'});
var input2 = schema.validate({input: {name:"x", email:"x@xyz.com"}});
var input3 = schema.validate({input: {name:123, email:"x@xyz.com"}});
var input4 = schema.validate({});
var input5 = schema.validate({input:null});
var input6 = schema2.validate({input:null});
expect(!input1.valid).to.be.ok;
expect(input2.valid).to.be.ok;
expect(input2.valid).to.be.ok;
expect(input4.valid).to.be.ok;
expect(!input5.valid).to.be.ok;
expect(input6.valid).to.be.ok;
});
});