Comparing version 1.0.2 to 1.0.3
import type { ParseDataType, ReWrap } from "../type-utils"; | ||
import type { AllDataTypes } from "../types"; | ||
/** | ||
* Higher order function that generates a validator which will | ||
* check the provided `data` against the `dataType` type | ||
* structure definition and returns a boolean indicating if the | ||
* check was successful. | ||
*/ | ||
export declare const createValidator: <DT extends AllDataTypes>(dataType: DT) => (data: unknown) => data is ReWrap<ParseDataType<DT>>; | ||
/** Function alias for the `createValidator`. */ | ||
export declare const createChecker: <DT extends AllDataTypes>(dataType: DT) => (data: unknown) => data is ReWrap<ParseDataType<DT>>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createChecker = void 0; | ||
var validate_type_1 = require("./validators/validate-type"); | ||
var createChecker = function (dataType) { | ||
return function (data) { | ||
exports.createChecker = exports.createValidator = void 0; | ||
const validate_type_1 = require("./validators/validate-type"); | ||
/** | ||
* Higher order function that generates a validator which will | ||
* check the provided `data` against the `dataType` type | ||
* structure definition and returns a boolean indicating if the | ||
* check was successful. | ||
*/ | ||
const createValidator = (dataType) => { | ||
const validator = (data) => { | ||
try { | ||
@@ -15,3 +21,6 @@ (0, validate_type_1.validateType)("$", dataType, data); | ||
}; | ||
return validator; | ||
}; | ||
exports.createChecker = createChecker; | ||
exports.createValidator = createValidator; | ||
/** Function alias for the `createValidator`. */ | ||
exports.createChecker = exports.createValidator; |
import type { AllDataTypes } from ".."; | ||
import type { ParseDataType, ReWrap } from "../type-utils"; | ||
import type { ValidationError } from "./validation-error/validation-error"; | ||
export declare const createValidatedFunction: <DT extends AllDataTypes, R, ER = void>(dataType: DT, onValidationSuccess: (data: ReWrap<ParseDataType<DT>>) => R, onValidationError?: ((error: ValidationError, passedData: unknown) => ER) | undefined) => (data: unknown) => R | ER | undefined; | ||
/** | ||
* Higher order function that generates a new function which will | ||
* check the provided `data` against the `dataType` type | ||
* structure, and if the check is successful then the first | ||
* callback `onValidationSuccess` is invoked with `data` as it's | ||
* argument, otherwise the second callback `onValidationError` is | ||
* invoked with the type validation error as it's argument | ||
* (unless the callback is not specified). | ||
*/ | ||
export declare const createTypeGuardedFunction: <DT extends AllDataTypes, R, ER = void>(dataType: DT, onValidationSuccess: (data: ReWrap<ParseDataType<DT>>) => R, onValidationError?: ((error: ValidationError, passedData: unknown) => ER) | undefined) => (data: unknown) => R | ER; | ||
/** Function alias for the `createTypeGuardedFunction`. */ | ||
export declare const createValidatedFunction: <DT extends AllDataTypes, R, ER = void>(dataType: DT, onValidationSuccess: (data: ReWrap<ParseDataType<DT>>) => R, onValidationError?: ((error: ValidationError, passedData: unknown) => ER) | undefined) => (data: unknown) => R | ER; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createValidatedFunction = void 0; | ||
var validate_type_1 = require("./validators/validate-type"); | ||
var createValidatedFunction = function (dataType, onValidationSuccess, onValidationError) { | ||
var call = function (data) { | ||
exports.createValidatedFunction = exports.createTypeGuardedFunction = void 0; | ||
const validate_type_1 = require("./validators/validate-type"); | ||
/** | ||
* Higher order function that generates a new function which will | ||
* check the provided `data` against the `dataType` type | ||
* structure, and if the check is successful then the first | ||
* callback `onValidationSuccess` is invoked with `data` as it's | ||
* argument, otherwise the second callback `onValidationError` is | ||
* invoked with the type validation error as it's argument | ||
* (unless the callback is not specified). | ||
*/ | ||
const createTypeGuardedFunction = (dataType, onValidationSuccess, onValidationError) => { | ||
const caller = (data) => { | ||
try { | ||
@@ -13,2 +22,3 @@ (0, validate_type_1.validateType)("$", dataType, data); | ||
catch (e) { | ||
// @ts-expect-error | ||
return onValidationError | ||
@@ -19,4 +29,6 @@ ? onValidationError(e, data) | ||
}; | ||
return call; | ||
return caller; | ||
}; | ||
exports.createValidatedFunction = createValidatedFunction; | ||
exports.createTypeGuardedFunction = createTypeGuardedFunction; | ||
/** Function alias for the `createTypeGuardedFunction`. */ | ||
exports.createValidatedFunction = exports.createTypeGuardedFunction; |
import type { AllDataTypes } from "../../types"; | ||
export declare class ValidationError extends Error { | ||
export declare class ValidationError extends TypeError { | ||
static isValidationError(e: unknown | ValidationError): e is ValidationError; | ||
@@ -4,0 +4,0 @@ private readonly _validation_error; |
"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.ValidationError = void 0; | ||
var ValidationError = /** @class */ (function (_super) { | ||
__extends(ValidationError, _super); | ||
function ValidationError(path, expected, value) { | ||
var _this = _super.call(this, "Invalid Data") || this; | ||
_this._validation_error = true; | ||
_this.expectedValueType = expected; | ||
_this.fieldPath = path; | ||
_this.receivedValue = value; | ||
return _this; | ||
class ValidationError extends TypeError { | ||
constructor(path, expected, value) { | ||
super("Value does not conform the data type structure definition."); | ||
this._validation_error = true; | ||
this.expectedValueType = expected; | ||
this.fieldPath = path; | ||
this.receivedValue = value; | ||
} | ||
ValidationError.isValidationError = function (e) { | ||
static isValidationError(e) { | ||
return (typeof e === "object" && | ||
@@ -34,5 +17,4 @@ e !== null && | ||
"_validation_error" in e); | ||
}; | ||
return ValidationError; | ||
}(Error)); | ||
} | ||
} | ||
exports.ValidationError = ValidationError; |
"use strict"; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateArray = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validate_one_of_1 = require("./validate-one-of"); | ||
var validateArray = function (path, type, data) { | ||
var e_1, _a; | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validate_one_of_1 = require("./validate-one-of"); | ||
const validateArray = (path, type, data) => { | ||
if (!Array.isArray(data)) | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
try { | ||
for (var _b = __values(data.entries()), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = __read(_c.value, 2), index = _d[0], elem = _d[1]; | ||
(0, validate_one_of_1.validateOneOf)("".concat(path, ".").concat(index), { oneOf: type.arrayOf }, elem); | ||
} | ||
for (const [index, elem] of data.entries()) { | ||
(0, validate_one_of_1.validateOneOf)(`${path}.${index}`, { oneOf: type.arrayOf }, elem); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}; | ||
exports.validateArray = validateArray; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateEnumMember = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validateEnumMember = function (path, type, data) { | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validateEnumMember = (path, type, data) => { | ||
if (type.enumMember !== data) { | ||
@@ -7,0 +7,0 @@ throw new validation_error_1.ValidationError(path, type, data); |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateEnum = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validateEnum = function (path, type, data) { | ||
var isDataEqualToAnyMember = Object.entries(type.enumInstance).some(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], member = _b[1]; | ||
return isNaN(Number(key)) && member === data; | ||
}); | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validateEnum = (path, type, data) => { | ||
const isDataEqualToAnyMember = Object.entries(type.enumInstance).some(([key, member]) => isNaN(Number(key)) && member === data); | ||
if (!isDataEqualToAnyMember) { | ||
@@ -27,0 +8,0 @@ throw new validation_error_1.ValidationError(path, type, data); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateLiteral = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validateLiteral = function (path, type, data) { | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validateLiteral = (path, type, data) => { | ||
if (type.literal === data) | ||
@@ -7,0 +7,0 @@ return; |
"use strict"; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateOneOf = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validate_type_1 = require("./validate-type"); | ||
var validateOneOf = function (path, type, data) { | ||
var e_1, _a; | ||
var passed = false; | ||
try { | ||
for (var _b = __values(type.oneOf), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var dataType = _c.value; | ||
try { | ||
(0, validate_type_1.validateType)(path, dataType, data); | ||
passed = true; | ||
break; | ||
} | ||
catch (e) { | ||
continue; | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validate_type_1 = require("./validate-type"); | ||
const validateOneOf = (path, type, data) => { | ||
let passed = false; | ||
for (const dataType of type.oneOf) { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
(0, validate_type_1.validateType)(path, dataType, data); | ||
passed = true; | ||
break; | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
catch (e) { | ||
continue; | ||
} | ||
} | ||
@@ -40,0 +18,0 @@ if (passed) |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validatePrimitive = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validatePrimitive = function (path, type, data) { | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validatePrimitive = (path, type, data) => { | ||
if (type === "unknown") | ||
@@ -10,3 +10,3 @@ return; | ||
return; | ||
var dataType = typeof data; | ||
const dataType = typeof data; | ||
if (dataType === type) | ||
@@ -13,0 +13,0 @@ return; |
"use strict"; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateRecord = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validate_type_1 = require("./validate-type"); | ||
var validateRecord = function (path, type, data) { | ||
var e_1, _a; | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validate_type_1 = require("./validate-type"); | ||
const validateRecord = (path, type, data) => { | ||
if (typeof data !== "object" || data === null) | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
try { | ||
for (var _b = __values(Object.entries(type.recordOf)), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = __read(_c.value, 2), key = _d[0], fieldDescriptor = _d[1]; | ||
if (!(key in data)) { | ||
if (fieldDescriptor.required === true || | ||
fieldDescriptor.required === undefined) { | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
} | ||
else { | ||
continue; | ||
} | ||
for (const [key, fieldDescriptor] of Object.entries(type.recordOf)) { | ||
if (!(key in data)) { | ||
if (fieldDescriptor.required === true || | ||
fieldDescriptor.required === undefined) { | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
} | ||
// @ts-expect-error | ||
var value = data[key]; | ||
(0, validate_type_1.validateType)("".concat(path, ".").concat(key), fieldDescriptor.type, value); | ||
else { | ||
continue; | ||
} | ||
} | ||
// @ts-expect-error | ||
const value = data[key]; | ||
(0, validate_type_1.validateType)(`${path}.${key}`, fieldDescriptor.type, value); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}; | ||
exports.validateRecord = validateRecord; |
"use strict"; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateSet = void 0; | ||
var validation_error_1 = require("../validation-error/validation-error"); | ||
var validate_one_of_1 = require("./validate-one-of"); | ||
var isSet = function (data) { | ||
const validation_error_1 = require("../validation-error/validation-error"); | ||
const validate_one_of_1 = require("./validate-one-of"); | ||
const isSet = (data) => { | ||
return (typeof data === "object" && | ||
@@ -23,20 +12,9 @@ data !== null && | ||
}; | ||
var validateSet = function (path, type, data) { | ||
var e_1, _a; | ||
const validateSet = (path, type, data) => { | ||
if (!isSet(data)) | ||
throw new validation_error_1.ValidationError(path, type, data); | ||
try { | ||
for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) { | ||
var elem = data_1_1.value; | ||
(0, validate_one_of_1.validateOneOf)("".concat(path, ".SET"), { oneOf: type.setOf }, elem); | ||
} | ||
for (const elem of data) { | ||
(0, validate_one_of_1.validateOneOf)(`${path}.SET`, { oneOf: type.setOf }, elem); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}; | ||
exports.validateSet = validateSet; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateType = void 0; | ||
var validate_array_1 = require("./validate-array"); | ||
var validate_enum_1 = require("./validate-enum"); | ||
var validate_enum_member_1 = require("./validate-enum-member"); | ||
var validate_literal_1 = require("./validate-literal"); | ||
var validate_one_of_1 = require("./validate-one-of"); | ||
var validate_primitive_1 = require("./validate-primitive"); | ||
var validate_record_1 = require("./validate-record"); | ||
var validate_set_1 = require("./validate-set"); | ||
var validateType = function (path, type, data) { | ||
const validate_array_1 = require("./validate-array"); | ||
const validate_enum_1 = require("./validate-enum"); | ||
const validate_enum_member_1 = require("./validate-enum-member"); | ||
const validate_literal_1 = require("./validate-literal"); | ||
const validate_one_of_1 = require("./validate-one-of"); | ||
const validate_primitive_1 = require("./validate-primitive"); | ||
const validate_record_1 = require("./validate-record"); | ||
const validate_set_1 = require("./validate-set"); | ||
const validateType = (path, type, data) => { | ||
if (typeof type === "string") { | ||
@@ -14,0 +14,0 @@ return (0, validate_primitive_1.validatePrimitive)(path, type, data); |
"use strict"; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validate = void 0; | ||
var validation_error_1 = require("../checker-machine/validation-error/validation-error"); | ||
var validate_type_1 = require("../checker-machine/validators/validate-type"); | ||
var validate = function (validator) { | ||
const validation_error_1 = require("../checker-machine/validation-error/validation-error"); | ||
const validate_type_1 = require("../checker-machine/validators/validate-type"); | ||
const validate = (validator) => { | ||
return function (_, propertyKey, descriptor) { | ||
var originalMethod = descriptor.value; | ||
const originalMethod = descriptor.value; | ||
descriptor.value = function (data) { | ||
var _this = this; | ||
var onError = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
const onError = (...args) => { | ||
if (this.onValidationError && | ||
typeof this.onValidationError === "function") { | ||
this.onValidationError(...args); | ||
} | ||
if (_this.onValidationError && | ||
typeof _this.onValidationError === "function") { | ||
_this.onValidationError.apply(_this, __spreadArray([], __read(args), false)); | ||
} | ||
}; | ||
@@ -46,0 +16,0 @@ try { |
@@ -1,5 +0,31 @@ | ||
export { createChecker } from "./checker-machine/create-checker"; | ||
export { createValidatedFunction } from "./checker-machine/create-validated-function"; | ||
declare const _default: { | ||
createChecker: <DT extends import("./types").AllDataTypes>(dataType: DT) => (data: unknown) => data is import("./type-utils").ReWrap<import("./type-utils").ParseDataType<DT>>; | ||
createValidator: <DT extends import("./types").AllDataTypes>(dataType: DT) => (data: unknown) => data is import("./type-utils").ReWrap<import("./type-utils").ParseDataType<DT>>; | ||
createTypeGuardedFunction: <DT_1 extends import("./types").AllDataTypes, R, ER = void>(dataType: DT_1, onValidationSuccess: (data: import("./type-utils").ReWrap<import("./type-utils").ParseDataType<DT_1>>) => R, onValidationError?: ((error: import("./checker-machine/validation-error/validation-error").ValidationError, passedData: unknown) => ER) | undefined) => (data: unknown) => R | ER; | ||
createValidatedFunction: <DT_1 extends import("./types").AllDataTypes, R, ER = void>(dataType: DT_1, onValidationSuccess: (data: import("./type-utils").ReWrap<import("./type-utils").ParseDataType<DT_1>>) => R, onValidationError?: ((error: import("./checker-machine/validation-error/validation-error").ValidationError, passedData: unknown) => ER) | undefined) => (data: unknown) => R | ER; | ||
ensureDataType: <DT_2 extends import("./types").AllDataTypes>(dataType: DT_2, data: unknown) => void; | ||
DataType: { | ||
RecordOf<TS extends import("./types").RecordTypeSchema>(args: TS): import("./types").RecordOf<TS>; | ||
ArrayOf<DT_3 extends import("./types").AllDataTypes[]>(...args: DT_3): import("./types").ArrayOf<DT_3>; | ||
SetOf<DT_4 extends import("./types").AllDataTypes[]>(...args: DT_4): import("./types").SetOf<DT_4>; | ||
OneOf<DT_5 extends import("./types").AllDataTypes[]>(...args: DT_5): import("./types").OneOf<DT_5>; | ||
Literal<V extends string | number | boolean>(value: V): import("./types").Literal<V>; | ||
EnumMember<M extends string | number>(enumMember: M): import("./types").EnumMember<M>; | ||
Enum<T extends string, TEnumValue extends string | number>(enumInstance: { [key in T]: TEnumValue; }): import("./types").Enum<TEnumValue>; | ||
Unknown: "unknown"; | ||
String: "string"; | ||
Number: "number"; | ||
Boolean: "boolean"; | ||
Symbol: "symbol"; | ||
Function: "function"; | ||
Null: "null"; | ||
Undefined: "undefined"; | ||
}; | ||
}; | ||
export default _default; | ||
export { createChecker, createValidator, } from "./checker-machine/create-checker"; | ||
export { createTypeGuardedFunction, createValidatedFunction, } from "./checker-machine/create-validated-function"; | ||
export { ensureDataType } from "./checker-machine/ensure-data-type"; | ||
export { DataType } from "./schame-construction-helpers"; | ||
export { GetDataType } from "./type-utils"; | ||
export { AllDataTypes, ArrayOf, BasicDataType, ComplexDataType, Enum, EnumMember, FieldDescriptor, Literal, OneOf, RecordOf, RecordTypeSchema as TypeSchema, SetOf, } from "./types"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.DataType = exports.createValidatedFunction = exports.createChecker = void 0; | ||
var create_checker_1 = require("./checker-machine/create-checker"); | ||
Object.defineProperty(exports, "createChecker", { enumerable: true, get: function () { return create_checker_1.createChecker; } }); | ||
var create_validated_function_1 = require("./checker-machine/create-validated-function"); | ||
Object.defineProperty(exports, "createValidatedFunction", { enumerable: true, get: function () { return create_validated_function_1.createValidatedFunction; } }); | ||
var schame_construction_helpers_1 = require("./schame-construction-helpers"); | ||
Object.defineProperty(exports, "DataType", { enumerable: true, get: function () { return schame_construction_helpers_1.DataType; } }); | ||
exports.DataType = exports.ensureDataType = exports.createValidatedFunction = exports.createTypeGuardedFunction = exports.createValidator = exports.createChecker = void 0; | ||
const create_checker_1 = require("./checker-machine/create-checker"); | ||
const create_validated_function_1 = require("./checker-machine/create-validated-function"); | ||
const ensure_data_type_1 = require("./checker-machine/ensure-data-type"); | ||
const schame_construction_helpers_1 = require("./schame-construction-helpers"); | ||
exports.default = { | ||
createChecker: create_checker_1.createChecker, | ||
createValidator: create_checker_1.createValidator, | ||
createTypeGuardedFunction: create_validated_function_1.createTypeGuardedFunction, | ||
createValidatedFunction: create_validated_function_1.createValidatedFunction, | ||
ensureDataType: ensure_data_type_1.ensureDataType, | ||
DataType: schame_construction_helpers_1.DataType, | ||
}; | ||
var create_checker_2 = require("./checker-machine/create-checker"); | ||
Object.defineProperty(exports, "createChecker", { enumerable: true, get: function () { return create_checker_2.createChecker; } }); | ||
Object.defineProperty(exports, "createValidator", { enumerable: true, get: function () { return create_checker_2.createValidator; } }); | ||
var create_validated_function_2 = require("./checker-machine/create-validated-function"); | ||
Object.defineProperty(exports, "createTypeGuardedFunction", { enumerable: true, get: function () { return create_validated_function_2.createTypeGuardedFunction; } }); | ||
Object.defineProperty(exports, "createValidatedFunction", { enumerable: true, get: function () { return create_validated_function_2.createValidatedFunction; } }); | ||
var ensure_data_type_2 = require("./checker-machine/ensure-data-type"); | ||
Object.defineProperty(exports, "ensureDataType", { enumerable: true, get: function () { return ensure_data_type_2.ensureDataType; } }); | ||
var schame_construction_helpers_2 = require("./schame-construction-helpers"); | ||
Object.defineProperty(exports, "DataType", { enumerable: true, get: function () { return schame_construction_helpers_2.DataType; } }); |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -25,29 +14,23 @@ exports.DataType = exports.BasicDataTypes = void 0; | ||
}; | ||
exports.DataType = __assign(__assign({}, exports.BasicDataTypes), { RecordOf: function (args) { | ||
exports.DataType = Object.assign(Object.assign({}, exports.BasicDataTypes), { RecordOf(args) { | ||
return { recordOf: args }; | ||
}, ArrayOf: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
}, | ||
ArrayOf(...args) { | ||
return { arrayOf: args }; | ||
}, SetOf: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
}, | ||
SetOf(...args) { | ||
return { setOf: args }; | ||
}, OneOf: function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
}, | ||
OneOf(...args) { | ||
return { oneOf: args }; | ||
}, Literal: function (value) { | ||
}, | ||
Literal(value) { | ||
return { literal: value }; | ||
}, EnumMember: function (enumMember) { | ||
return { enumMember: enumMember }; | ||
}, Enum: function (enumInstance) { | ||
}, | ||
EnumMember(enumMember) { | ||
return { enumMember }; | ||
}, | ||
Enum(enumInstance) { | ||
// @ts-expect-error | ||
return { enumInstance: enumInstance }; | ||
return { enumInstance }; | ||
} }); |
{ | ||
"name": "dilswer", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "./dist/index.js", | ||
"exports": { | ||
".": "./dist", | ||
"./decorators": "./dist/decorators/index.js" | ||
}, | ||
"license": "MIT", | ||
@@ -10,0 +6,0 @@ "repository": { |
# Dilswer | ||
<font size="4"> | ||
Small and lightweight data validation library with TypeScript integration. | ||
</font> | ||
Small and lightweight data validation library with TypeScript integration. | ||
<br> | ||
Keep your type definitions in one place, and have but one source of truth for | ||
@@ -130,2 +126,58 @@ both the runtime validation types and the TypeScript type definitions. | ||
## Dilswer Exports | ||
#### dilswer.createValidator() | ||
```ts | ||
const createValidator: <DT extends AllDataTypes>( | ||
dataType: DT | ||
) => (data: unknown) => data is ParseDataType<DT>; | ||
``` | ||
Higher order function that generates a validator which will check the provided | ||
`data` against the `dataType` type structure definition and returns a boolean | ||
indicating if the check was successful. | ||
#### dilswer.createChecker() | ||
Alias for the `createValidator()`. | ||
#### dilswer.createTypeGuardedFunction() | ||
```ts | ||
const createTypeGuardedFunction: <DT extends AllDataTypes, R, ER = void>( | ||
dataType: DT, | ||
onValidationSuccess: (data: ReWrap<ParseDataType<DT>>) => R, | ||
onValidationError?: (error: ValidationError, data: unknown) => ER | ||
) => (data: unknown) => R | ER; | ||
``` | ||
Higher order function that generates a new function which will check the | ||
provided `data` against the `dataType` type structure, and if the check is | ||
successful then the first callback `onValidationSuccess` is invoked with `data` | ||
as it's argument, otherwise the second callback `onValidationError` is invoked | ||
with the type validation error as it's argument (unless the callback is not | ||
specified). | ||
#### dilswer.createValidatedFunction() | ||
Alias for the `createTypeGuardedFunction()`. | ||
#### dilswer.ensureDataType() | ||
```ts | ||
const ensureDataType: <DT extends AllDataTypes>( | ||
dataType: DT, | ||
data: unknown | ||
) => void; | ||
``` | ||
Checks the provided `data` against the `dataType` type definition and throws an | ||
ValidationError if the `data` does not conform to the `dataType`. | ||
#### dilswer.DataType | ||
Object containing all the dilswer runtime type definitions (like `Number`, | ||
`String`, `ArrayOf(...)`, etc.) | ||
## Data Types | ||
@@ -132,0 +184,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43
343
38773
586