one-schema
Version:
one-schema object validator
473 lines (472 loc) • 18.6 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.VALID_TYPES = exports.FieldValidatorArray = exports.FieldValidatorObject = exports.FieldValidatorFloat64 = exports.FieldValidatorInt32 = exports.FieldValidatorString = exports.FieldValidatorBool = exports.FieldValidatorBase = void 0;
var validatorManager_1 = require("./validatorManager");
var VALID_TYPES = ['bool', 'string', 'int32', 'float64', 'object', 'array'];
exports.VALID_TYPES = VALID_TYPES;
var BASIC_TYPE_CHECKS = {
bool: function (x) { return typeof x === 'boolean'; },
string: function (x) { return (typeof x === 'string' || x === null); },
int32: function (x) { return typeof x === 'number' && !Number.isNaN(x) && Number.isFinite(x) && Math.round(x) === x; },
float64: function (x) { return typeof x === 'number'; },
object: function (x) { return typeof x === 'object'; },
array: function (x) { return Array.isArray(x); },
};
function basicCheckType(type, v) {
if (BASIC_TYPE_CHECKS.hasOwnProperty(type) && typeof BASIC_TYPE_CHECKS[type] === 'function') {
return BASIC_TYPE_CHECKS[type](v);
}
return false;
}
var FieldValidatorBase = /** @class */ (function () {
function FieldValidatorBase() {
this.settings = {};
}
FieldValidatorBase.prototype.required = function (required) {
if (required === void 0) { required = true; }
if (typeof this.settings.type === 'undefined') {
throw new Error('Must set type before setting options');
}
this.settings.required = !!required;
return this;
};
FieldValidatorBase.prototype.defaultValue = function (_inputValue) {
throw new Error("You must set type before setting the defaultValue");
return this;
};
FieldValidatorBase.prototype.tags = function (tags) {
if (typeof this.settings.type === 'undefined') {
throw new Error('Must set type before setting the example');
}
if (!Array.isArray(tags) || !tags.length || tags.filter(function (t) { return (typeof t !== 'string' || !t.length); }).length) {
throw new Error('Field Validator tags must be an array of non-empty strings!');
}
this.settings.tags = tags;
return this;
};
FieldValidatorBase.prototype.description = function (description) {
if (typeof this.settings.type === 'undefined') {
throw new Error('Must set type before setting the example');
}
if (typeof description !== 'string') {
throw new Error("'description' must be a string!");
}
this.settings.description = description;
return this;
};
FieldValidatorBase.prototype.example = function (_example) {
throw new Error("You must set type before setting the example");
return this;
};
FieldValidatorBase.prototype.validator = function (validator) {
if (typeof this.settings.type === 'undefined') {
throw new Error('Must set type before setting the example');
}
var tmpValidator = typeof validator === 'string' ? ({ id: validator }) : validator;
if (typeof tmpValidator !== 'object' || !tmpValidator || !tmpValidator.id) {
throw new Error('Invalid validator!');
}
if (!validatorManager_1.hasValidatorIdForType(tmpValidator.id, this.settings.type)) {
throw new Error('Validator not registered!');
}
this.settings.validator = tmpValidator;
return this;
};
return FieldValidatorBase;
}());
exports.FieldValidatorBase = FieldValidatorBase;
var FieldValidatorBool = /** @class */ (function (_super) {
__extends(FieldValidatorBool, _super);
function FieldValidatorBool(base) {
var _this = _super.call(this) || this;
base.type = 'bool';
_this.settings = base;
return _this;
}
FieldValidatorBool.prototype.defaultValue = function (inputValue) {
if (!basicCheckType(this.settings.type, inputValue)) {
throw new Error('Invalid defaultValue value');
}
this.settings.defaultValue = inputValue;
return this;
};
FieldValidatorBool.prototype.example = function (example) {
if (!basicCheckType(this.settings.type, example)) {
throw new Error('Invalid example value');
}
this.settings.example = example;
return this;
};
FieldValidatorBool.prototype.oneOf = function (inputValues) {
var _this = this;
// TODO: More rigorous check if the elements in oneOf are valid, for now use type info
if (Array.isArray(inputValues)) {
inputValues.forEach(function (v) {
if (!basicCheckType(_this.settings.type, v)) {
throw new Error("Invalid oneOf value '" + v + "'");
}
});
this.settings.oneOf = inputValues.concat([]);
}
else {
this.settings.oneOf = undefined;
}
return this;
};
return FieldValidatorBool;
}(FieldValidatorBase));
exports.FieldValidatorBool = FieldValidatorBool;
var FieldValidatorString = /** @class */ (function (_super) {
__extends(FieldValidatorString, _super);
function FieldValidatorString(base) {
var _this = _super.call(this) || this;
base.type = 'string';
_this.settings = base;
return _this;
}
FieldValidatorString.prototype.defaultValue = function (inputValue) {
if (!basicCheckType(this.settings.type, inputValue)) {
throw new Error('Invalid defaultValue value');
}
this.settings.defaultValue = inputValue;
return this;
};
FieldValidatorString.prototype.example = function (example) {
if (!basicCheckType(this.settings.type, example)) {
throw new Error('Invalid example value');
}
this.settings.example = example;
return this;
};
FieldValidatorString.prototype.max = function (inputMax) {
if (typeof inputMax !== 'number') {
throw new Error("Invalid max, must be type number");
}
this.settings.max = inputMax;
return this;
};
FieldValidatorString.prototype.min = function (inputMin) {
if (typeof inputMin !== 'number') {
throw new Error("Invalid min, must be type number");
}
this.settings.min = inputMin;
return this;
};
FieldValidatorString.prototype.allowNull = function (allowNull) {
if (allowNull === void 0) { allowNull = true; }
this.settings.allowNull = !!allowNull;
return this;
};
FieldValidatorString.prototype.primaryKey = function (primaryKey) {
if (primaryKey === void 0) { primaryKey = true; }
this.settings.primaryKey = !!primaryKey;
return this;
};
FieldValidatorString.prototype.autoIncrement = function (autoIncrement) {
if (autoIncrement === void 0) { autoIncrement = true; }
this.settings.autoIncrement = !!autoIncrement;
return this;
};
FieldValidatorString.prototype.oneOf = function (inputValues) {
var _this = this;
// TODO: More rigorous check if the elements in oneOf are valid, for now use type info
if (Array.isArray(inputValues)) {
inputValues.forEach(function (v) {
if (!basicCheckType(_this.settings.type, v)) {
throw new Error("Invalid oneOf value '" + v + "'");
}
});
this.settings.oneOf = inputValues.concat([]);
}
else {
this.settings.oneOf = undefined;
}
return this;
};
FieldValidatorString.prototype.uuid = function (isUuid) {
if (isUuid === void 0) { isUuid = true; }
this.settings.uuid = isUuid;
return this;
};
return FieldValidatorString;
}(FieldValidatorBase));
exports.FieldValidatorString = FieldValidatorString;
var FieldValidatorInt32 = /** @class */ (function (_super) {
__extends(FieldValidatorInt32, _super);
function FieldValidatorInt32(base) {
var _this = _super.call(this) || this;
base.type = 'int32';
_this.settings = base;
return _this;
}
FieldValidatorInt32.prototype.defaultValue = function (inputValue) {
if (!basicCheckType(this.settings.type, inputValue)) {
throw new Error('Invalid defaultValue value');
}
this.settings.defaultValue = inputValue;
return this;
};
FieldValidatorInt32.prototype.example = function (example) {
if (!basicCheckType(this.settings.type, example)) {
throw new Error('Invalid example value');
}
this.settings.example = example;
return this;
};
FieldValidatorInt32.prototype.max = function (inputMax) {
if (typeof inputMax !== 'number') {
throw new Error("Invalid max, must be type number");
}
this.settings.max = inputMax;
return this;
};
FieldValidatorInt32.prototype.min = function (inputMin) {
if (typeof inputMin !== 'number') {
throw new Error("Invalid min, must be type number");
}
this.settings.min = inputMin;
return this;
};
FieldValidatorInt32.prototype.primaryKey = function (primaryKey) {
if (primaryKey === void 0) { primaryKey = true; }
this.settings.primaryKey = !!primaryKey;
return this;
};
FieldValidatorInt32.prototype.autoIncrement = function (autoIncrement) {
if (autoIncrement === void 0) { autoIncrement = true; }
this.settings.autoIncrement = !!autoIncrement;
return this;
};
FieldValidatorInt32.prototype.oneOf = function (inputValues) {
var _this = this;
// TODO: More rigorous check if the elements in oneOf are valid, for now use type info
if (Array.isArray(inputValues)) {
inputValues.forEach(function (v) {
if (!basicCheckType(_this.settings.type, v)) {
throw new Error("Invalid oneOf value '" + v + "'");
}
});
this.settings.oneOf = inputValues.concat([]);
}
else {
this.settings.oneOf = undefined;
}
return this;
};
return FieldValidatorInt32;
}(FieldValidatorBase));
exports.FieldValidatorInt32 = FieldValidatorInt32;
var FieldValidatorFloat64 = /** @class */ (function (_super) {
__extends(FieldValidatorFloat64, _super);
function FieldValidatorFloat64(base) {
var _this = _super.call(this) || this;
base.type = 'float64';
_this.settings = base;
return _this;
}
FieldValidatorFloat64.prototype.defaultValue = function (inputValue) {
if (!basicCheckType(this.settings.type, inputValue)) {
throw new Error('Invalid defaultValue value');
}
this.settings.defaultValue = inputValue;
return this;
};
FieldValidatorFloat64.prototype.example = function (example) {
if (!basicCheckType(this.settings.type, example)) {
throw new Error('Invalid example value');
}
this.settings.example = example;
return this;
};
FieldValidatorFloat64.prototype.max = function (inputMax) {
if (typeof inputMax !== 'number') {
throw new Error("Invalid max, must be type number");
}
this.settings.max = inputMax;
return this;
};
FieldValidatorFloat64.prototype.min = function (inputMin) {
if (typeof inputMin !== 'number') {
throw new Error("Invalid min, must be type number");
}
this.settings.min = inputMin;
return this;
};
FieldValidatorFloat64.prototype.oneOf = function (inputValues) {
var _this = this;
// TODO: More rigorous check if the elements in oneOf are valid, for now use type info
if (Array.isArray(inputValues)) {
inputValues.forEach(function (v) {
if (!basicCheckType(_this.settings.type, v)) {
throw new Error("Invalid oneOf value '" + v + "'");
}
});
this.settings.oneOf = inputValues.concat([]);
}
else {
this.settings.oneOf = undefined;
}
return this;
};
FieldValidatorFloat64.prototype.allowNaN = function (allowNaN) {
if (allowNaN === void 0) { allowNaN = true; }
this.settings.allowNaN = allowNaN;
return this;
};
return FieldValidatorFloat64;
}(FieldValidatorBase));
exports.FieldValidatorFloat64 = FieldValidatorFloat64;
var FieldValidatorObject = /** @class */ (function (_super) {
__extends(FieldValidatorObject, _super);
function FieldValidatorObject(base) {
var _this = _super.call(this) || this;
base.type = 'object';
_this.settings = base;
return _this;
}
FieldValidatorObject.prototype.defaultValue = function (inputValue) {
if (!basicCheckType(this.settings.type, inputValue)) {
throw new Error('Invalid defaultValue value');
}
this.settings.defaultValue = inputValue;
return this;
};
FieldValidatorObject.prototype.example = function (example) {
if (!basicCheckType(this.settings.type, example)) {
throw new Error('Invalid example value');
}
this.settings.example = example;
return this;
};
FieldValidatorObject.prototype.schema = function (inputSchema) {
this.settings.schema = inputSchema;
return this;
};
return FieldValidatorObject;
}(FieldValidatorBase));
exports.FieldValidatorObject = FieldValidatorObject;
var FieldValidatorArray = /** @class */ (function (_super) {
__extends(FieldValidatorArray, _super);
function FieldValidatorArray(base) {
var _this = _super.call(this) || this;
base.type = 'array';
_this.settings = base;
return _this;
}
FieldValidatorArray.prototype.defaultValue = function (inputValue) {
if (!basicCheckType(this.settings.type, inputValue)) {
throw new Error('Invalid defaultValue value');
}
this.settings.defaultValue = inputValue;
return this;
};
FieldValidatorArray.prototype.example = function (example) {
if (!basicCheckType(this.settings.type, example)) {
throw new Error('Invalid example value');
}
this.settings.example = example;
return this;
};
FieldValidatorArray.prototype.max = function (inputMax) {
if (typeof inputMax !== 'number') {
throw new Error("Invalid max, must be type number");
}
this.settings.max = inputMax;
return this;
};
FieldValidatorArray.prototype.min = function (inputMin) {
if (typeof inputMin !== 'number') {
throw new Error("Invalid min, must be type number");
}
this.settings.min = inputMin;
return this;
};
FieldValidatorArray.prototype.arrayOf = function (inputSchema) {
if (!inputSchema || !inputSchema.settings || !inputSchema.settings.type || VALID_TYPES.indexOf(inputSchema.settings.type) === -1) {
throw new Error("Invalid input schema provided to arrayOf: input undefined or input type undefined/not set!");
}
this.settings.arrayOf = inputSchema;
return this;
};
return FieldValidatorArray;
}(FieldValidatorBase));
exports.FieldValidatorArray = FieldValidatorArray;
function assignToThis(inputThis, fv) {
Object.keys(fv).forEach(function (k) {
if (typeof fv[k] === 'function') {
inputThis[k] = fv[k].bind(fv);
}
else {
inputThis[k] = fv[k];
}
});
return fv;
}
var FieldValidatorStart = /** @class */ (function (_super) {
__extends(FieldValidatorStart, _super);
function FieldValidatorStart() {
return _super.call(this) || this;
}
FieldValidatorStart.prototype.bool = function () {
return assignToThis(this, new FieldValidatorBool({}));
};
FieldValidatorStart.prototype.string = function () {
return assignToThis(this, new FieldValidatorString({}));
};
FieldValidatorStart.prototype.int32 = function () {
return assignToThis(this, new FieldValidatorInt32({}));
};
FieldValidatorStart.prototype.float64 = function () {
return assignToThis(this, new FieldValidatorFloat64({}));
};
FieldValidatorStart.prototype.object = function (schema) {
var fv = new FieldValidatorObject({});
if (schema) {
return assignToThis(this, fv.schema(schema));
}
else {
return assignToThis(this, fv);
}
};
FieldValidatorStart.prototype.obj = function (schema) {
return this.object(schema);
};
FieldValidatorStart.prototype.array = function () {
return assignToThis(this, new FieldValidatorArray({}));
};
FieldValidatorStart.prototype.type = function (inputType) {
if (typeof inputType !== 'string' ||
VALID_TYPES.indexOf(inputType) === -1 ||
typeof this[inputType] !== 'function' ||
(inputType + "") === 'type') {
throw new Error("Invalid type " + inputType);
}
return this[inputType]();
};
FieldValidatorStart.prototype.arrayOf = function (schema) {
var fv = new FieldValidatorArray({});
if (schema) {
return assignToThis(this, fv.arrayOf(schema));
}
else {
return assignToThis(this, fv);
}
};
return FieldValidatorStart;
}(FieldValidatorBase));
exports.default = FieldValidatorStart;