Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x | const Ajv = require('ajv');
class UtAjv extends Ajv {
constructor(options) {
super(options);
this.addKeyword({
keyword: 'x-file',
compile: schema => value => {
const isFile = value && value.constructor.name === 'File';
return schema === true ? isFile : !isFile;
},
metaSchema: {
description: 'x-file',
type: 'boolean'
}
});
this.addKeyword({
keyword: 'x-required',
compile: schema => value => {
return schema === true ? typeof value !== 'undefined' : true;
},
metaSchema: {
description: 'x-required',
type: 'boolean'
}
});
this.addKeyword({
keyword: 'x-occurrences',
type: 'array',
errors: true,
compile: originalSchema => {
const errors = [];
const schema = originalSchema.map(originalRule => {
// don't override rule by reference
const rule = Object.assign({}, originalRule);
if (rule.pattern) {
try {
rule.pattern = new RegExp(rule.pattern);
} catch (e) {
errors.push({
message: `pattern "${rule.pattern}" must be a valid javascript pattern`
});
}
}
return rule;
});
if (errors.length) {
const error = new Error('x-occurrences');
error.errors = errors;
throw error;
}
return function validate(value) {
const errors = [];
if (value.length === 0) {
errors.push('Array must not be empty');
} else {
for (let i = 0; i < schema.length; i += 1) {
const rule = schema[i];
const matches = [];
for (let j = 0; j < value.length; j += 1) {
const record = value[j];
if (!record[rule.key]) {
errors.push(`${rule.key} was not provided on index ${j}`);
continue;
}
if (rule.pattern) {
if (typeof record[rule.key] !== 'string') {
errors.push(`The value of ${rule.key} on index ${j} must be a string matching the pattern ${rule.pattern}`);
continue;
}
if (rule.pattern.test(record[rule.key])) {
matches.push(j);
}
} else {
if (record[rule.key] === rule.value) {
matches.push(j);
}
}
}
if (matches.length < rule.min) {
errors.push(`Rule (min: ${rule.min}) violated! matches: ${matches.length}, key ${rule.key}`);
} else if (matches.length > rule.max) {
errors.push(`Rule (max: ${rule.max}) violated! matches: ${matches.length}, key ${rule.key}`);
}
}
}
if (errors.length !== 0) {
validate.errors = errors.map(message => {
return {
keyword: 'x-occurrences',
message,
params: {
keyword: 'x-occurrences'
}
};
});
return false;
}
return true;
};
},
metaSchema: {
type: 'array',
minItems: 1,
items: {
type: 'object',
required: ['key', 'min', 'max'],
additionalProperties: false,
maxProperties: 4,
minProperties: 4,
properties: {
key: {
description: 'key',
type: 'string'
},
min: {
description: 'min',
type: 'integer',
minimum: 0
},
max: {
description: 'max',
type: 'integer',
minimum: {
$data: '1/min'
}
},
value: {
description: 'value'
},
pattern: {
description: 'pattern',
minLength: 1,
type: 'string'
}
}
}
}
});
}
}
module.exports = UtAjv;
|