1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var rules = require("./rules"),
|
14 | environments = require("../conf/environments"),
|
15 | schemaValidator = require("is-my-json-valid");
|
16 |
|
17 | var validators = {
|
18 | rules: Object.create(null)
|
19 | };
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function getRuleOptionsSchema(id) {
|
27 | var rule = rules.get(id),
|
28 | schema = rule && rule.schema;
|
29 |
|
30 | if (!schema) {
|
31 | return {
|
32 | "type": "array",
|
33 | "items": [
|
34 | {
|
35 | "enum": [0, 1, 2]
|
36 | }
|
37 | ],
|
38 | "minItems": 1
|
39 | };
|
40 | }
|
41 |
|
42 |
|
43 | if (Array.isArray(schema)) {
|
44 | return {
|
45 | "type": "array",
|
46 | "items": [
|
47 | {
|
48 | "enum": [0, 1, 2]
|
49 | }
|
50 | ].concat(schema),
|
51 | "minItems": 1,
|
52 | "maxItems": schema.length + 1
|
53 | };
|
54 | }
|
55 |
|
56 |
|
57 | return schema;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function validateRuleOptions(id, options, source) {
|
68 | var validateRule = validators.rules[id],
|
69 | message;
|
70 |
|
71 | if (!validateRule) {
|
72 | validateRule = schemaValidator(getRuleOptionsSchema(id), { verbose: true });
|
73 | validators.rules[id] = validateRule;
|
74 | }
|
75 |
|
76 | if (typeof options === "number") {
|
77 | options = [options];
|
78 | }
|
79 |
|
80 | validateRule(options);
|
81 |
|
82 | if (validateRule.errors) {
|
83 | message = [
|
84 | source, ":\n",
|
85 | "\tConfiguration for rule \"", id, "\" is invalid:\n"
|
86 | ];
|
87 | validateRule.errors.forEach(function(error) {
|
88 | if (error.field === "data[\"0\"]") {
|
89 | message.push(
|
90 | "\tSeverity should be one of the following: 0 = off, 1 = warning, 2 = error (you passed \"", error.value, "\").\n");
|
91 | } else {
|
92 | message.push(
|
93 | "\tValue \"", error.value, "\" ", error.message, ".\n"
|
94 | );
|
95 | }
|
96 | });
|
97 |
|
98 | throw new Error(message.join(""));
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function validateEnvironment(environment, source) {
|
109 |
|
110 |
|
111 | if (!environment) {
|
112 | return;
|
113 | }
|
114 |
|
115 | if (Array.isArray(environment)) {
|
116 | throw new Error("Environment must not be an array");
|
117 | }
|
118 |
|
119 | if (typeof environment === "object") {
|
120 | Object.keys(environment).forEach(function(env) {
|
121 | if (!environments[env]) {
|
122 | var message = [
|
123 | source, ":\n",
|
124 | "\tEnvironment key \"", env, "\" is unknown\n"
|
125 | ];
|
126 | throw new Error(message.join(""));
|
127 | }
|
128 | });
|
129 | } else {
|
130 | throw new Error("Environment must be an object");
|
131 | }
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function validate(config, source) {
|
141 |
|
142 | if (typeof config.rules === "object") {
|
143 | Object.keys(config.rules).forEach(function(id) {
|
144 | validateRuleOptions(id, config.rules[id], source);
|
145 | });
|
146 | }
|
147 |
|
148 | validateEnvironment(config.env, source);
|
149 | }
|
150 |
|
151 | module.exports = {
|
152 | getRuleOptionsSchema: getRuleOptionsSchema,
|
153 | validate: validate,
|
154 | validateRuleOptions: validateRuleOptions
|
155 | };
|