Skip to content

Commit 26abbd9

Browse files
committed
anyOf, enum, maxItems, minItems, maxLength, minLength, maxProperties, minProperties, multipleOf, oneOf, pattern rules
1 parent 008a93f commit 26abbd9

16 files changed

+292
-43
lines changed

lib/compile/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
var doT = require('dot')
4-
, fs = require('fs');
4+
, fs = require('fs')
5+
, stableStringify = require('json-stable-stringify');
56

67
var RULES = require('./rules')
78
, _validateTemplateStr = fs.readFileSync(__dirname + '/_validate.dot.js')
@@ -19,6 +20,8 @@ function compile(schema) {
1920
RULES: RULES,
2021
_validate: _validateTemplate,
2122
copy: copy,
23+
getDataType: getDataType,
24+
stableStringify: stableStringify,
2225
opts: this.opts
2326
});
2427
// console.log('\n\n\n *** \n', validateCode);

lib/compile/rules/anyOf.dot.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var errors = [];
5+
6+
{{~ it.schema:$schema:$i }}
7+
{{
8+
var $it = it.copy(it);
9+
$it.schema = $schema;
10+
$it.schemaPath = it.schemaPath + '[' + $i + ']';
11+
}}
12+
13+
var result = ({{= it._validate($it) }})(data, dataType, dataPath);
14+
15+
if (result.valid) {
16+
return { valid: true, errors: [] };
17+
} else {
18+
errors.push.apply(errors, result.errors);
19+
}
20+
{{~}}
21+
22+
return { valid: false, errors: errors };
23+
}

lib/compile/rules/enum.dot.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var errors = [];
5+
6+
{{
7+
var $simpleTypes = [ 'boolean', 'null', 'number', 'string' ]
8+
, $itemsHash = { 'boolean': {}, 'null': {}, 'number': {}, 'string': {} };
9+
10+
var $onlySimpleTypes = it.schema.every(function ($item) {
11+
var $itemType = typeof $item;
12+
var $isSimpleType = $simpleTypes.indexOf($itemType) >= 0;
13+
if ($isSimpleType) $itemsHash[$itemType][$item] = true;
14+
return $isSimpleType;
15+
});
16+
17+
if (!$onlySimpleTypes) {
18+
var $itemsList = it.schema.map(function ($item) {
19+
return {
20+
type: it.getDataType($item),
21+
valueStr: it.stableStringify($item)
22+
}
23+
});
24+
}
25+
}}
26+
27+
{{? $onlySimpleTypes }}
28+
var itemsHash = {{= JSON.stringify($itemsHash) }};
29+
var valid = itemsHash[dataType] && itemsHash[dataType][data];
30+
{{??}}
31+
var itemsList = {{= JSON.stringify($itemsList) }};
32+
for (var i = 0; i < itemsList.length; i++) {
33+
var item = itemsList[i];
34+
var valid = dataType == item.type && stableStringify(data) == item.valueStr;
35+
if (valid) break;
36+
}
37+
{{?}}
38+
39+
return {
40+
valid: valid || false,
41+
errors: valid ? [] : [{
42+
keyword: 'enum',
43+
schema: self.schema{{= it.schemaPath }},
44+
dataPath: dataPath,
45+
message: 'data' + dataPath + ' is not valid, should be equal to one of values in the schema'
46+
{{? it.opts.verbose }}, data: data{{?}}
47+
}]
48+
};
49+
}

lib/compile/rules/index.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ var fs = require('fs')
55

66
var RULES = module.exports = {
77
type: { code: fs.readFileSync(__dirname + '/type.dot.js') },
8-
// enum: { code: '' },
8+
enum: { code: fs.readFileSync(__dirname + '/enum.dot.js') },
99
allOf: { code: fs.readFileSync(__dirname + '/allOf.dot.js') },
10-
// anyOf: { code: '' },
11-
// oneOf: { code: '' },
10+
anyOf: { code: fs.readFileSync(__dirname + '/anyOf.dot.js') },
11+
oneOf: { code: fs.readFileSync(__dirname + '/oneOf.dot.js') },
1212
not: { code: fs.readFileSync(__dirname + '/not.dot.js') },
1313
maximum: {
1414
code: fs.readFileSync(__dirname + '/maximum.dot.js'),
@@ -18,22 +18,22 @@ var RULES = module.exports = {
1818
code: fs.readFileSync(__dirname + '/minimum.dot.js'),
1919
type: 'number'
2020
},
21-
// multipleOf: {
22-
// code: '',
23-
// type: 'number'
24-
// },
25-
// maxLength: {
26-
// code: '',
27-
// type: 'string'
28-
// },
29-
// minLength: {
30-
// code: '',
31-
// type: 'string'
32-
// },
33-
// pattern: {
34-
// code: '',
35-
// type: 'string'
36-
// },
21+
multipleOf: {
22+
code: fs.readFileSync(__dirname + '/multipleOf.dot.js'),
23+
type: 'number'
24+
},
25+
maxLength: {
26+
code: fs.readFileSync(__dirname + '/maxLength.dot.js'),
27+
type: 'string'
28+
},
29+
minLength: {
30+
code: fs.readFileSync(__dirname + '/minLength.dot.js'),
31+
type: 'string'
32+
},
33+
pattern: {
34+
code: fs.readFileSync(__dirname + '/pattern.dot.js'),
35+
type: 'string'
36+
},
3737
// additionalItems: {
3838
// code: '',
3939
// type: 'array'
@@ -42,26 +42,26 @@ var RULES = module.exports = {
4242
// code: '',
4343
// type: 'array'
4444
// },
45-
// maxItems: {
46-
// code: '',
47-
// type: 'array'
48-
// },
49-
// minItems: {
50-
// code: '',
51-
// type: 'array'
52-
// },
45+
maxItems: {
46+
code: fs.readFileSync(__dirname + '/maxItems.dot.js'),
47+
type: 'array'
48+
},
49+
minItems: {
50+
code: fs.readFileSync(__dirname + '/minItems.dot.js'),
51+
type: 'array'
52+
},
5353
// uniqueItems: {
5454
// code: '',
5555
// type: 'array'
5656
// },
57-
// maxProperties: {
58-
// code: '',
59-
// type: 'object'
60-
// },
61-
// minProperties: {
62-
// code: '',
63-
// type: 'object'
64-
// },
57+
maxProperties: {
58+
code: fs.readFileSync(__dirname + '/maxProperties.dot.js'),
59+
type: 'object'
60+
},
61+
minProperties: {
62+
code: fs.readFileSync(__dirname + '/minProperties.dot.js'),
63+
type: 'object'
64+
},
6565
required: {
6666
code: fs.readFileSync(__dirname + '/required.dot.js'),
6767
type: 'object'

lib/compile/rules/maxItems.dot.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var valid = data.length <= {{= it.schema }};
5+
6+
return {
7+
valid: valid,
8+
errors: valid ? [] : [{
9+
keyword: 'maxItems',
10+
schema: {{= it.schema }},
11+
dataPath: dataPath,
12+
message: 'data' + dataPath + ' is not valid, should NOT have more than {{= it.schema }} items'
13+
{{? it.opts.verbose }}, data: data{{?}}
14+
}]
15+
};
16+
}

lib/compile/rules/maxLength.dot.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var valid = data.length <= {{= it.schema }};
5+
6+
return {
7+
valid: valid,
8+
errors: valid ? [] : [{
9+
keyword: 'maxLength',
10+
schema: {{= it.schema }},
11+
dataPath: dataPath,
12+
message: 'data' + dataPath + ' is not valid, should NOT be longer than {{= it.schema }} characters'
13+
{{? it.opts.verbose }}, data: data{{?}}
14+
}]
15+
};
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var propertiesNum = Object.keys(data).length;
5+
var valid = propertiesNum <= {{= it.schema }};
6+
7+
return {
8+
valid: valid,
9+
errors: valid ? [] : [{
10+
keyword: 'maxProperties',
11+
schema: {{= it.schema }},
12+
dataPath: dataPath,
13+
message: 'data' + dataPath + ' is not valid, should NOT have more than {{= it.schema }} properties'
14+
{{? it.opts.verbose }}, data: data{{?}}
15+
}]
16+
};
17+
}

lib/compile/rules/minItems.dot.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var valid = data.length >= {{= it.schema }};
5+
6+
return {
7+
valid: valid,
8+
errors: valid ? [] : [{
9+
keyword: 'minItems',
10+
schema: {{= it.schema }},
11+
dataPath: dataPath,
12+
message: 'data' + dataPath + ' is not valid, should NOT have less than {{= it.schema }} items'
13+
{{? it.opts.verbose }}, data: data{{?}}
14+
}]
15+
};
16+
}

lib/compile/rules/minLength.dot.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var valid = data.length >= {{= it.schema }};
5+
6+
return {
7+
valid: valid,
8+
errors: valid ? [] : [{
9+
keyword: 'minLength',
10+
schema: {{= it.schema }},
11+
dataPath: dataPath,
12+
message: 'data' + dataPath + ' is not valid, should NOT be shorter than {{= it.schema }} characters'
13+
{{? it.opts.verbose }}, data: data{{?}}
14+
}]
15+
};
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function (data, dataType, dataPath) {
2+
'use strict';
3+
4+
var propertiesNum = Object.keys(data).length;
5+
var valid = propertiesNum >= {{= it.schema }};
6+
7+
return {
8+
valid: valid,
9+
errors: valid ? [] : [{
10+
keyword: 'minProperties',
11+
schema: {{= it.schema }},
12+
dataPath: dataPath,
13+
message: 'data' + dataPath + ' is not valid, should NOT have less than {{= it.schema }} properties'
14+
{{? it.opts.verbose }}, data: data{{?}}
15+
}]
16+
};
17+
}

0 commit comments

Comments
 (0)