"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Any: () => Any, Array: () => Array, Boolean: () => Boolean, COMPILE_KEY: () => COMPILE_KEY, Currency: () => Currency, Custom: () => Custom, Date: () => Date, Email: () => Email, Enum: () => Enum, Equal: () => Equal, Field: () => Field, Func: () => Func, Instance: () => Instance, Luhn: () => Luhn, Mac: () => Mac, Multi: () => Multi, Nested: () => Nested, NestedArray: () => NestedArray, Number: () => Number, ObjectId: () => ObjectId, SCHEMA_KEY: () => SCHEMA_KEY, Schema: () => Schema, String: () => String, UUID: () => UUID, Url: () => Url, decoratorFactory: () => decoratorFactory, getPrototypeChain: () => getPrototypeChain, getSchema: () => getSchema, updateSchema: () => updateSchema, validate: () => validate, validateOrReject: () => validateOrReject }); module.exports = __toCommonJS(src_exports); var import_reflect_metadata = require("reflect-metadata"); // src/constants.ts var SCHEMA_KEY = Symbol("propertyMetadata"); var COMPILE_KEY = Symbol("compileKey"); // src/utils/get-prototype-chain.ts function getPrototypeChain(object) { let proto = object; const protos = [ object ]; while (proto) { proto = Object.getPrototypeOf(proto); if (proto) { protos.push(proto); } } return protos; } __name(getPrototypeChain, "getPrototypeChain"); // src/utils/get-schema.ts function getSchema(klass) { const chain = getPrototypeChain(klass.prototype); const schema = {}; Object.assign(schema, ...chain.map((c) => Reflect.getOwnMetadata(SCHEMA_KEY, c))); return schema; } __name(getSchema, "getSchema"); // src/utils/update-schema.ts function updateSchema(target, key, options) { const s = Reflect.getOwnMetadata(SCHEMA_KEY, target) ?? {}; const alreadyHasDecorator = s[key] !== void 0; if (alreadyHasDecorator) { const isAlreadyMulti = s[key].type === "multi"; if (isAlreadyMulti) { s[key].rules.push(options); return Reflect.defineMetadata(SCHEMA_KEY, s, target); } s[key] = { type: "multi", rules: [ s[key], options ] }; return Reflect.defineMetadata(SCHEMA_KEY, s, target); } s[key] = options; Reflect.defineMetadata(SCHEMA_KEY, s, target); } __name(updateSchema, "updateSchema"); // src/decorators.ts var decoratorFactory = /* @__PURE__ */ __name((mandatory = {}) => { return function(...options) { return (target, key) => { updateSchema(target, key, { ...options[0], ...mandatory }); }; }; }, "decoratorFactory"); var Field = decoratorFactory({}); var String = decoratorFactory({ type: "string" }); var Multi = decoratorFactory({ type: "multi" }); var Boolean = decoratorFactory({ type: "boolean" }); var Number = decoratorFactory({ type: "number" }); var UUID = decoratorFactory({ type: "uuid" }); var ObjectId = decoratorFactory({ type: "objectID" }); var Email = decoratorFactory({ type: "email" }); var Date = decoratorFactory({ type: "date" }); var Enum = decoratorFactory({ type: "enum" }); var Array = decoratorFactory({ type: "array" }); var Any = decoratorFactory({ type: "any" }); var Equal = decoratorFactory({ type: "equal" }); var Instance = decoratorFactory({ type: "class" }); var Currency = decoratorFactory({ type: "currency" }); var Func = decoratorFactory({ type: "function" }); var Luhn = decoratorFactory({ type: "luhn" }); var Mac = decoratorFactory({ type: "mac" }); var Url = decoratorFactory({ type: "url" }); var Custom = decoratorFactory({ type: "custom" }); function getNestedObject(schemaClass, options) { const props = Object.assign({}, getSchema(schemaClass)); const strict = props.$$strict || false; delete props.$$strict; delete props.$$async; return { ...options, props, strict, type: "object" }; } __name(getNestedObject, "getNestedObject"); function wrapIntoArray(items, options) { return { type: "array", ...options, items }; } __name(wrapIntoArray, "wrapIntoArray"); function Nested(options = {}) { return (target, key) => { const t = options.validator ?? Reflect.getMetadata("design:type", target, key); if (!t) { throw new Error(`Cannot get "design:type" of ${key}. Please use the validator option to @Nested`); } delete options.validator; updateSchema(target, key, getNestedObject(t, options)); }; } __name(Nested, "Nested"); function NestedArray(options) { return (target, key) => { const validator = options.validator; delete options.validator; updateSchema(target, key, wrapIntoArray(getNestedObject(validator, {}), options)); }; } __name(NestedArray, "NestedArray"); // src/schema.ts var import_fastest_validator = __toESM(require("fastest-validator"), 1); function Schema(schemaOptions, validatorOptions = {}) { return /* @__PURE__ */ __name(function _Schema(target) { updateSchema(target.prototype, "$$strict", (schemaOptions == null ? void 0 : schemaOptions.strict) ?? false); if ((schemaOptions == null ? void 0 : schemaOptions.async) !== void 0) { updateSchema(target.prototype, "$$async", schemaOptions.async); } if (schemaOptions) { for (const element of Object.keys(schemaOptions).filter((key) => key !== "async" && key !== "strict")) { updateSchema(target.prototype, element, schemaOptions[element]); } } const s = getSchema(target); const v = new import_fastest_validator.default({ ...validatorOptions, useNewCustomCheckerFunction: true }); Reflect.defineMetadata(COMPILE_KEY, v.compile({ ...s }), target); return target; }, "_Schema"); } __name(Schema, "Schema"); // src/utils/validate.ts var validate = /* @__PURE__ */ __name((schemaInstance) => { const validate2 = Reflect.getOwnMetadata(COMPILE_KEY, schemaInstance.constructor); if (!validate2) { throw new Error("Obj is missing complied validation method"); } return validate2(schemaInstance); }, "validate"); // src/utils/validate-or-reject.ts var validateOrReject = /* @__PURE__ */ __name(async (schemaInstance) => { const result = await validate(schemaInstance); if (result !== true) { throw result; } return true; }, "validateOrReject"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Any, Array, Boolean, COMPILE_KEY, Currency, Custom, Date, Email, Enum, Equal, Field, Func, Instance, Luhn, Mac, Multi, Nested, NestedArray, Number, ObjectId, SCHEMA_KEY, Schema, String, UUID, Url, decoratorFactory, getPrototypeChain, getSchema, updateSchema, validate, validateOrReject });