@nestia/core
Version:
Super-fast validation decorators of NestJS
86 lines • 3.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validate_request_query = void 0;
const common_1 = require("@nestjs/common");
const typia_1 = __importDefault(require("typia"));
const NoTransformConfigurationError_1 = require("../NoTransformConfigurationError");
/** @internal */
const validate_request_query = (method) => (validator) => {
if (!validator) {
(0, NoTransformConfigurationError_1.NoTransformConfigurationError)(method);
return (input) => Object.fromEntries(input.entries());
}
else if (validator.type === "assert")
return assert(validator.assert);
else if (validator.type === "is")
return is(validator.is);
else if (validator.type === "validate")
return validate(validator.validate);
return () => new Error(`Error on nestia.core.${method}(): invalid typed validator.`);
};
exports.validate_request_query = validate_request_query;
/** @internal */
const assert = (closure) => (input) => {
try {
return closure(input);
}
catch (exp) {
if ((() => {
const _io0 = input => "string" === typeof input.name && "string" === typeof input.message && (undefined === input.stack || "string" === typeof input.stack) && true && "string" === typeof input.method && (undefined === input.path || "string" === typeof input.path) && "string" === typeof input.expected && true && (undefined === input.description || "string" === typeof input.description);
return input => "object" === typeof input && null !== input && _io0(input);
})()(exp)) {
return new common_1.BadRequestException({
path: exp.path,
reason: exp.message,
expected: exp.expected,
value: exp.value,
message: MESSAGE,
});
}
if (is_missing_query_property(exp))
return new common_1.BadRequestException(MESSAGE);
throw exp;
}
};
/** @internal */
const is = (closure) => (input) => {
const result = (() => {
try {
return closure(input);
}
catch (exp) {
if (is_missing_query_property(exp))
return null;
throw exp;
}
})();
return result !== null ? result : new common_1.BadRequestException(MESSAGE);
};
/** @internal */
const validate = (closure) => (input) => {
const result = (() => {
try {
return closure(input);
}
catch (exp) {
if (is_missing_query_property(exp))
return null;
throw exp;
}
})();
if (result === null)
return new common_1.BadRequestException(MESSAGE);
return result.success
? result.data
: new common_1.BadRequestException({
errors: result.errors,
message: MESSAGE,
});
};
/** @internal */
const MESSAGE = "Request query data is not following the promised type.";
const is_missing_query_property = (exp) => exp instanceof Error && /^missing [^.\[\]\s]+$/.test(exp.message);
//# sourceMappingURL=validate_request_query.js.map