/
array.js
92 lines (80 loc) · 2.48 KB
/
array.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
'use strict';
var type = require('type')
, deepEqual = require('../deepequal')
module.exports = validateArray;
function validateArray(){
var valid = true
if (type(this.instance())!=='array') return (valid);
valid = validateArrayLength.call(this) && valid;
valid = validateArrayUniqueItems.call(this) && valid;
valid = validateArrayItems.call(this) && valid;
return (valid);
}
function validateArrayLength(){
var min = this.property('minItems')
, max = this.property('maxItems')
, instance = this.instance()
, valid = true
if (min){
valid = this.assert(instance.length >= min,
"has less than the minimum number of items",
"minItems"
) && valid;
}
if (max){
valid = this.assert(instance.length <= max,
"has greater than the maximum number of items",
"maxItems"
) && valid;
}
return (valid);
}
function validateArrayUniqueItems(){
var unique = this.property('uniqueItems')
, instance = this.instance()
, match = false
if (!unique) return (!match);
for (var i=0;i<instance.length;++i){
for (var j=i+1;j<instance.length;++j){
match = deepEqual(instance[i],instance[j]);
if (match) break;
}
if (match) break;
}
this.assert(!match,
"does not contain unique items",
"uniqueItems"
);
return (!match);
}
function validateArrayItems(){
var items = this.get('items')
, additional = this.property('additionalItems')
, additionalSchema = this.get('additionalItems')
, instance = this.instance()
, valid = true;
if (!items) return (valid);
if (items.nodeType == 'SchemaArray'){
for (var i=0;i<instance.length;++i){
var schema = items.get(i)
if (schema){
var ctx = this.subcontext(['items',i].join('/'),i)
valid = ctx.validate() && valid;
} else if (type(additional)=='boolean') {
valid = this.assert(additional,
"contains additional items",
"additionalItems"
) && valid;
} else if (additionalSchema){
var ctx = this.subcontext('additionalItems',i)
valid = ctx.validate() && valid;
}
}
} else if (items.nodeType == 'Schema') {
for (var i=0;i<instance.length;++i){
var ctx = this.subcontext('items',i)
valid = ctx.validate() && valid
}
}
return (valid);
}