feat: add used defaults and patterns to validation function to support ajv-pack, #156

master
Evgeny Poberezkin 2016-09-24 15:58:14 +01:00
parent 33527596ec
commit 5193d45fdc
3 changed files with 28 additions and 22 deletions

View File

@ -168,7 +168,13 @@ function compile(schema, root, localRefs, baseId) {
validate.refVal = refVal;
validate.root = isRoot ? validate : _root;
if ($async) validate.$async = true;
if (keepSourceCode) validate.sourceCode = sourceCode;
if (keepSourceCode) {
validate.sourceCode = sourceCode;
validate.source = {
patterns: patterns,
defaults: defaults
};
}
return validate;
}

20
lib/compile/ucs2length.js Normal file
View File

@ -0,0 +1,20 @@
'use strict';
// https://mathiasbynens.be/notes/javascript-encoding
// https://github.com/bestiejs/punycode.js - punycode.ucs2.decode
module.exports = function ucs2length(str) {
var length = 0
, len = str.length
, pos = 0
, value;
while (pos < len) {
length++;
value = str.charCodeAt(pos++);
if (value >= 0xD800 && value <= 0xDBFF && pos < len) {
// high surrogate, and there is a next character
value = str.charCodeAt(pos);
if ((value & 0xFC00) == 0xDC00) pos++; // low surrogate
}
}
return length;
};

View File

@ -9,7 +9,7 @@ module.exports = {
toHash: toHash,
getProperty: getProperty,
escapeQuotes: escapeQuotes,
ucs2length: ucs2length,
ucs2length: require('./ucs2length'),
varOccurences: varOccurences,
varReplace: varReplace,
cleanUpCode: cleanUpCode,
@ -120,26 +120,6 @@ function escapeQuotes(str) {
}
// https://mathiasbynens.be/notes/javascript-encoding
// https://github.com/bestiejs/punycode.js - punycode.ucs2.decode
function ucs2length(str) {
var length = 0
, len = str.length
, pos = 0
, value;
while (pos < len) {
length++;
value = str.charCodeAt(pos++);
if (value >= 0xD800 && value <= 0xDBFF && pos < len) {
// high surrogate, and there is a next character
value = str.charCodeAt(pos);
if ((value & 0xFC00) == 0xDC00) pos++; // low surrogate
}
}
return length;
}
function varOccurences(str, dataVar) {
dataVar += '[^0-9]';
var matches = str.match(new RegExp(dataVar, 'g'));