to-json-schema
Version:
Convert JS object to JSON Schema
95 lines (87 loc) • 5.42 kB
JavaScript
; // content of this file is extracted from jsonschema source
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
var types = {
string: function testString(instance) {
return typeof instance === 'string';
},
number: function testNumber(instance) {
// isFinite returns false for NaN, Infinity, and -Infinity
return typeof instance === 'number' && isFinite(instance); // eslint-disable-line no-restricted-globals
},
integer: function testInteger(instance) {
return typeof instance === 'number' && instance % 1 === 0;
},
"boolean": function testBoolean(instance) {
return typeof instance === 'boolean';
},
array: function testArray(instance) {
return instance instanceof Array;
},
"null": function testNull(instance) {
return instance === null;
},
date: function testDate(instance) {
return instance instanceof Date;
},
any:
/* istanbul ignore next: not using this but keeping it here for sake of completeness */
function testAny(instance) {
// eslint-disable-line no-unused-vars
return true;
},
object: function testObject(instance) {
return instance && _typeof(instance) === 'object' && !(instance instanceof Array) && !(instance instanceof Date);
}
};
var FORMAT_REGEXPS = {
'date-time': /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-(3[01]|0[1-9]|[12][0-9])[tT ](2[0-4]|[01][0-9]):([0-5][0-9]):(60|[0-5][0-9])(\.\d+)?([zZ]|[+-]([0-5][0-9]):(60|[0-5][0-9]))$/,
date: /^\d{4}-(?:0[0-9]{1}|1[0-2]{1})-(3[01]|0[1-9]|[12][0-9])$/,
time: /^(2[0-4]|[01][0-9]):([0-5][0-9]):(60|[0-5][0-9])$/,
email: /^(?:[\w!#$%&'*+-/=?^`{|}~]+\.)*[\w!#$%&'*+-/=?^`{|}~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-](?!$)){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?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/,
uri: /^[a-zA-Z][a-zA-Z0-9+-.]*:[^\s]*$/,
color: /^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/,
// hostname regex from: http://stackoverflow.com/a/1420225/5628
hostname: /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/,
'host-name': /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/,
alpha: /^[a-zA-Z]+$/,
alphanumeric: /^[a-zA-Z0-9]+$/,
'utc-millisec': function utcMillisec(input) {
return typeof input === 'string' && parseFloat(input) === parseInt(input, 10) && !isNaN(input);
},
// eslint-disable-line no-restricted-globals
regex
/* istanbul ignore next: not supporting regex right now */
: function regex(input) {
// eslint-disable-line space-before-function-paren
var result = true;
try {
new RegExp(input); // eslint-disable-line no-new
} catch (e) {
result = false;
}
return result;
},
style: /\s*(.+?):\s*([^;]+);?/g,
phone: /^\+(?:[0-9] ?){6,14}[0-9]$/
};
FORMAT_REGEXPS.regexp = FORMAT_REGEXPS.regex;
FORMAT_REGEXPS.pattern = FORMAT_REGEXPS.regex;
FORMAT_REGEXPS.ipv4 = FORMAT_REGEXPS['ip-address'];
var isFormat = function isFormat(input, format) {
if (typeof input === 'string' && FORMAT_REGEXPS[format] !== undefined) {
if (FORMAT_REGEXPS[format] instanceof RegExp) {
return FORMAT_REGEXPS[format].test(input);
}
if (typeof FORMAT_REGEXPS[format] === 'function') {
return FORMAT_REGEXPS[format](input);
}
}
return true;
};
module.exports = {
types: types,
isFormat: isFormat,
FORMAT_REGEXPS: FORMAT_REGEXPS
};