UNPKG

@yamamotok/dataobject

Version:

Decorator based JSON serializer and deserializer.

44 lines 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = void 0; function validate(opts) { var _a, _b; const { options, key, targetValue } = opts; const validator = (_a = options === null || options === void 0 ? void 0 : options.validator) === null || _a === void 0 ? void 0 : _a.validator; if (!validator) { return undefined; } const validatorOptions = (_b = options === null || options === void 0 ? void 0 : options.validator) === null || _b === void 0 ? void 0 : _b.options; const value = (() => { if (validatorOptions === null || validatorOptions === void 0 ? void 0 : validatorOptions.asString) { if (targetValue === undefined || targetValue === null) { return ''; } else { return String(targetValue); } } return targetValue; })(); let errorCause = undefined; try { const res = validator(value); if (res === false) { errorCause = { key, error: `${key} validation failed` }; } else if (res instanceof Error) { errorCause = { key, error: res }; } } catch (err) { if (err instanceof Error || typeof err === 'string') { errorCause = { key, error: err }; } else { errorCause = { key, error: 'Unknown error' }; } } return errorCause; } exports.validate = validate; //# sourceMappingURL=validate.js.map