ascertain 2.0.0-rc.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.cjs +2 -2
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +213 -2
- package/build/index.js +2 -2
- package/build/index.js.map +1 -1
- package/package.json +15 -12
package/build/index.cjs
CHANGED
|
@@ -273,14 +273,14 @@ ${codeGenExpectNoErrors(errorsAlias)}
|
|
|
273
273
|
return `
|
|
274
274
|
const ${valueAlias} = ${valuePath};
|
|
275
275
|
const ${registryAlias} = ctx.registry[${index}];
|
|
276
|
-
if (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for "path", expected symbol\`); }
|
|
276
|
+
if (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected symbol\`); }
|
|
277
277
|
if (${valueAlias} !== ${registryAlias}) { throw new TypeError(\`Invalid value \${${valueAlias}.toString()} for path "${path}", expected ${schema.toString()}\`); }
|
|
278
278
|
`;
|
|
279
279
|
} else if (schema === null || schema === undefined) {
|
|
280
280
|
const valueAlias = context.unique('v');
|
|
281
281
|
return `
|
|
282
282
|
const ${valueAlias} = ${valuePath};
|
|
283
|
-
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\`Invalid value ${valueAlias} for path "${path}", expected nullable\`); }
|
|
283
|
+
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\`Invalid value \${JSON.stringify(${valueAlias})} for path "${path}", expected nullable\`); }
|
|
284
284
|
`;
|
|
285
285
|
} else {
|
|
286
286
|
const valueAlias = context.unique('v');
|
package/build/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Abstract base class for schema operators.\n *\n * Provides a common constructor that enforces having at least one schema.\n *\n * @template T - The type of data the operator validates.\n * @abstract\n * @internal\n */\nabstract class Operator<T> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\n// https://standardschema.dev/\n\n/**\n * Symbol for validating object keys against a schema.\n */\nexport const $keys = Symbol.for('@@keys');\n/**\n * Symbol for validating object values against a schema.\n */\nexport const $values = Symbol.for('@@values');\n/**\n * Symbol for enforcing strict object validation (no extra properties allowed).\n */\nexport const $strict = Symbol.for('@@strict');\n\n/**\n * Represents a schema for validating data.\n *\n * Schemas can be defined for various data types, including objects, arrays, and primitives.\n *\n * @template T - The type of data the schema validates.\n */\nexport type Schema<T> =\n T extends Record<string | number | symbol, unknown>\n ? { [K in keyof T]?: Schema<T[K]> | unknown } & { [$keys]?: Schema<keyof T> } & { [$values]?: Schema<T[keyof T]> } & { [$strict]?: boolean }\n : T extends Array<infer A>\n ? Schema<A>[] | unknown\n : unknown;\n\nclass Or<T> extends Operator<T> {}\n/**\n * Operator for validating data against any of the provided schemas (logical OR).\n *\n * @template T - The type of data the operator validates.\n */\nexport const or = <T>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T> extends Operator<T> {}\n/**\n * Operator for validating data against all provided schemas (logical AND).\n *\n * @template T - The type of data the operator validates.\n */\nexport const and = <T>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T> extends Operator<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\n/**\n * Operator for making a schema optional (nullable).\n *\n * @template T - The type of data the operator validates.\n */\nexport const optional = <T>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T> extends Operator<T> {}\n/**\n * Operator for validating data against a fixed-length tuple of schemas.\n *\n * @template T - The type of data the operator validates (a tuple of types).\n */\nexport const tuple = <T>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const asError = <T>(message: string) => new TypeError(message) as unknown as T;\n\nexport const as = {\n /**\n * Attempts to convert a value to a string.\n *\n * @param value - The value to convert.\n * @returns The value as a string, or a TypeError if not a string.\n */\n string: (value: string | undefined): string => {\n return typeof value === 'string' ? value : asError(`Invalid value \"${value}\", expected a string`);\n },\n /**\n * Attempts to convert a value to a number.\n *\n * @param value - The value to convert (expected to be a string representation of a number).\n * @returns The value as a number, or a TypeError if not a valid number.\n */\n number: (value: string | undefined): number => {\n const result = parseFloat(value as string);\n return Number.isNaN(result) ? asError(`Invalid value ${value}, expected a valid number`) : result;\n },\n /**\n * Attempts to convert a value to a Date object.\n *\n * @param value - The value to convert (expected to be a string representation of a date).\n * @returns The value as a Date object, or a TypeError if no a valid date.\n */\n date: (value: string | undefined): Date => {\n const result = Date.parse(value as string);\n const date = new Date(result);\n return Number.isNaN(date.valueOf()) ? asError(`Invalid value \"${value}\", expected a valid date format`) : date;\n },\n /**\n * Attempts to convert a value to a time duration in milliseconds.\n *\n * @param value - The value to convert (e.g., \"5s\" for 5 seconds).\n * @param conversionFactor - Optional factor to divide the result by (default is 1).\n * @returns The time duration in milliseconds, or a TypeError if the format is invalid.\n */\n time: (value: string | undefined, conversionFactor = 1): number => {\n const matches = value?.match(/^(\\d*\\.?\\d*)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(`${(parseFloat(amount) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS]) / conversionFactor}`);\n }\n return asError(`Invalid value ${value}, expected a valid time format`);\n },\n /**\n * Attempts to convert a value to a boolean.\n *\n * @param value - The boolean like value to convert (e.g., \"true\", \"1\", \"enabled\").\n * @returns The value as a boolean, or a TypeError if it could not be converted to a boolean.\n */\n boolean: (value: string | undefined): boolean =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string)\n ? /^(1|true|enabled)$/i.test(value as string)\n : asError(`Invalid value ${value}, expected a boolean like`),\n /**\n * Attempts to convert a string into an array of strings by splitting it using the given delimiter.\n *\n * @param value - The string value to attempt to split into an array.\n * @param delimiter - The character or string used to separate elements in the input string.\n * @returns An array of strings if the conversion is successful, or a TypeError if the value is not a string.\n */\n array: (value: string | undefined, delimiter: string): string[] => value?.split?.(delimiter) ?? asError(`Invalid value ${value}, expected an array`),\n /**\n * Attempts to parse a JSON string into a JavaScript object.\n *\n * @template T - The expected type of the parsed JSON object.\n * @param value - The JSON string to attempt to parse.\n * @returns The parsed JSON object if successful, or a TypeError if the value is not valid JSON.\n */\n json: <T = object>(value: string | undefined): T => {\n try {\n return JSON.parse(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid JSON string`);\n }\n },\n /**\n * Attempts to decode a base64-encoded string.\n *\n * @param value - The base64-encoded string to attempt to decode.\n * @returns The decoded string if successful, or a TypeError if the value is not valid base64.\n */\n base64: (value: string | undefined): string => {\n try {\n return fromBase64(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid base64 string`);\n }\n },\n};\n\n/**\n * A class representing the context for schema validation.\n *\n * Stores a registry of values encountered during validation and provides methods for managing it.\n * @internal\n */\nclass Context {\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nconst codeGenCollectErrors = (errorsAlias: string, code: string, extra: string = '') => `try {${code}} catch (e) {${errorsAlias}.push(e.message);${extra}}`;\nconst codeGenExpectNoErrors = (errorsAlias: string) => `if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`;\nconst codeGenExpectNonError = (valueAlias: string, path: string) =>\n `if (${valueAlias} instanceof Error) { throw new TypeError(\\`\\${${valueAlias}.message} for path \"${path}\".\\`); }`;\nconst codeGenExpectNonNullable = (valueAlias: string, path: string) =>\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`;\nconst codeGenExpectObject = (valueAlias: string, path: string, instanceOf: string) =>\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of ${instanceOf}\\`); }`;\nconst codeGenExpectArray = (valueAlias: string, path: string) =>\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`;\n\nconst codeGen = <T>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n ${codeGenExpectNoErrors(errorsAlias)}\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight((result, code) => codeGenCollectErrors(errorsAlias, code, result), codeGenExpectNoErrors(errorsAlias));\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n `if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\\`Invalid tuple length \\${${valueAlias}.length} for path \"${path}\", expected ${schema.schemas.length}.\\`); }`,\n ...schema.schemas.map((s, idx) => codeGenCollectErrors(errorsAlias, codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`))),\n codeGenExpectNoErrors(errorsAlias),\n ];\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `const ${registryAlias} = ctx.registry[${index}];`,\n codeGenExpectNonNullable(valueAlias, path),\n ];\n if ((schema as unknown) !== Error && !(schema?.prototype instanceof Error)) {\n code.push(codeGenExpectNonError(valueAlias, path));\n }\n\n code.push(\n `if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected an instance of ${schema?.name}\\`); }`,\n `if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\\`Invalid type \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected type ${schema?.name}\\`); }`,\n `if (Number.isNaN(${valueAlias}?.valueOf?.())) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected a valid ${schema?.name}\\`); }`,\n );\n return code.join('\\n');\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectNonError(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) => `${valueAlias}.forEach((${value},${key}) => { ${codeGenCollectErrors(errorsAlias, codeGen(s, context, value, `${path}[\\${${key}}]`))} });`,\n ),\n );\n\n code.push(codeGenExpectNoErrors(errorsAlias));\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\n${codeGenExpectNonNullable(valueAlias, path)}\n${codeGenExpectNonError(valueAlias, path)}\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected to match ${schema.toString()}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Object'),\n codeGenExpectNonError(valueAlias, path),\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('k');\n const errorsAlias = context.unique('err');\n const kAlias = context.unique('k');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = [];\n${keysAlias}.forEach(${kAlias} => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$keys], context, kAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const kAlias = context.unique('k');\n const entriesAlias = context.unique('en');\n const errorsAlias = context.unique('err');\n code.push(`\nconst ${entriesAlias} = Object.entries(${valueAlias});\nconst ${errorsAlias} = [];\n${entriesAlias}.forEach(([${kAlias},${vAlias}]) => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$values], context, vAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($strict in schema && schema[$strict]) {\n const keysAlias = context.unique('k');\n const kAlias = context.unique('k');\n const extraAlias = context.unique('ex');\n code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);\n code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);\n code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\\`Extra properties: \\${${extraAlias}}, are not allowed for path \"${path}\"\\`); }`);\n }\n code.push(...Object.entries(schema).map(([key, s]) => codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `${code.join('\\n')}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for \"path\", expected symbol\\`); }\nif (${valueAlias} !== ${registryAlias}) { throw new TypeError(\\`Invalid value \\${${valueAlias}.toString()} for path \"${path}\", expected ${schema.toString()}\\`); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\\`Invalid value ${valueAlias} for path \"${path}\", expected nullable\\`); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${value} = ${JSON.stringify(schema)};\n${codeGenExpectNonError(valueAlias, path)}\nif (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected ${typeof schema}\\`); }\nif (${valueAlias} !== ${value}) { throw new TypeError(\\`Invalid value \\${JSON.stringify(${valueAlias})} for path \"${path}\", expected ${JSON.stringify(schema)}\\`); }\n`;\n }\n};\n\n/**\n * Compiles a schema into a validation function.\n *\n * This function takes a schema definition and generates a JavaScript function\n * that can be used to validate data against the schema.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to compile.\n * @param rootName - A name for the root of the data structure (used in error messages).\n * @returns A validation function that takes data as input and throws a TypeError if the data does not conform to the schema.\n */\nexport const compile = <T>(schema: Schema<T>, rootName: string) => {\n const context = new Context();\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: T) => validator(context, data);\n};\n\n/**\n * Asserts that data conforms to a given schema.\n *\n * This function is a convenient wrapper around `compile`. It compiles the schema\n * and immediately validates the provided data against it.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to validate against.\n * @param data - The data to validate.\n * @param rootName - A name for the root of the data structure (used in error messages, defaults to '[root]').\n * @throws {TypeError} If the data does not conform to the schema.\n */\nexport const ascertain = <T>(schema: Schema<T>, data: T, rootName = '[root]') => {\n compile(schema, rootName)(data);\n};\n"],"names":["$keys","$strict","$values","and","as","asError","ascertain","compile","fromBase64","optional","or","tuple","Operator","schemas","length","TypeError","name","Symbol","for","Or","And","Optional","schema","Tuple","Buffer","value","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","message","string","number","result","parseFloat","Number","isNaN","date","Date","parse","valueOf","time","conversionFactor","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","JSON","base64","Context","registry","varIndex","register","includes","push","indexOf","unique","prefix","codeGenCollectErrors","errorsAlias","code","extra","codeGenExpectNoErrors","codeGenExpectNonError","valueAlias","path","codeGenExpectNonNullable","codeGenExpectObject","instanceOf","codeGenExpectArray","codeGen","context","valuePath","map","join","reduceRight","idx","index","registryAlias","Error","prototype","Array","isArray","key","RegExp","keysAlias","kAlias","vAlias","entriesAlias","extraAlias","stringify","Object","keys","entries","undefined","rootName","validator","Function","data"],"mappings":";;;;;;;;;;;QAsBaA;eAAAA;;QAQAC;eAAAA;;QAJAC;eAAAA;;QAkCAC;eAAAA;;QAmCAC;eAAAA;;QAFAC;eAAAA;;QAoUAC;eAAAA;;QAnBAC;eAAAA;;QA5TAC;eAAAA;;QAVAC;eAAAA;;QApBAC;eAA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rC;IAC9C,OAAO,CAACsC,OAAYF,UAAUzB,SAAS2B;AACzC;AAcO,MAAMzG,YAAY,CAAIgB,QAAmByF,MAASH,WAAW,QAAQ;IAC1ErG,QAAQe,QAAQsF,UAAUG;AAC5B"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Abstract base class for schema operators.\n *\n * Provides a common constructor that enforces having at least one schema.\n *\n * @template T - The type of data the operator validates.\n * @abstract\n * @internal\n */\nabstract class Operator<T> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\n// https://standardschema.dev/\n\n/**\n * Symbol for validating object keys against a schema.\n */\nexport const $keys = Symbol.for('@@keys');\n/**\n * Symbol for validating object values against a schema.\n */\nexport const $values = Symbol.for('@@values');\n/**\n * Symbol for enforcing strict object validation (no extra properties allowed).\n */\nexport const $strict = Symbol.for('@@strict');\n\n/**\n * Represents a schema for validating data.\n *\n * Schemas can be defined for various data types, including objects, arrays, and primitives.\n *\n * @template T - The type of data the schema validates.\n */\nexport type Schema<T> =\n T extends Record<string | number | symbol, unknown>\n ? { [K in keyof T]?: Schema<T[K]> | unknown } & { [$keys]?: Schema<keyof T> } & { [$values]?: Schema<T[keyof T]> } & { [$strict]?: boolean }\n : T extends Array<infer A>\n ? Schema<A>[] | unknown\n : unknown;\n\nclass Or<T> extends Operator<T> {}\n/**\n * Operator for validating data against any of the provided schemas (logical OR).\n *\n * Creates a schema that accepts data matching any one of the provided schemas.\n * This is useful for creating union types or alternative validation paths.\n *\n * @template T - The type of data the operator validates.\n * @param schemas - Multiple schemas where at least one must match the data.\n * @returns A schema that validates data against any of the provided schemas.\n *\n * @example\n * ```typescript\n * import { or, ascertain } from 'ascertain';\n *\n * // Create a schema that accepts either a string or number\n * const stringOrNumber = or(String, Number);\n *\n * ascertain(stringOrNumber, \"hello\", \"value\"); // ✓ Valid\n * ascertain(stringOrNumber, 42, \"value\"); // ✓ Valid\n * ascertain(stringOrNumber, true, \"value\"); // ✗ Throws error\n *\n * // Union of literal values\n * const statusSchema = or('pending', 'completed', 'failed');\n * ascertain(statusSchema, 'pending', \"status\"); // ✓ Valid\n *\n * // Complex schema combinations\n * const userIdSchema = or(Number, { id: Number, temp: Boolean });\n * ascertain(userIdSchema, 123, \"userId\"); // ✓ Valid\n * ascertain(userIdSchema, { id: 456, temp: true }, \"userId\"); // ✓ Valid\n * ```\n */\nexport const or = <T>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T> extends Operator<T> {}\n/**\n * Operator for validating data against all provided schemas (logical AND).\n *\n * Creates a schema that requires data to match every one of the provided schemas.\n * This is useful for combining multiple validation requirements or adding constraints.\n *\n * @template T - The type of data the operator validates.\n * @param schemas - Multiple schemas that all must match the data.\n * @returns A schema that validates data against all of the provided schemas.\n *\n * @example\n * ```typescript\n * import { and, ascertain } from 'ascertain';\n *\n * // Combine object schema with additional constraints\n * const userSchema = and(\n * { name: String, age: Number },\n * { age: Number } // Additional constraint\n * );\n *\n * ascertain(userSchema, { name: \"John\", age: 25 }, \"user\"); // ✓ Valid\n *\n * // Ensure an object is both a Date and has specific methods\n * const validDateSchema = and(Date, { toISOString: Function });\n * ascertain(validDateSchema, new Date(), \"date\"); // ✓ Valid\n *\n * // Multiple validation layers\n * const positiveNumberSchema = and(Number, (n: number) => n > 0);\n * ascertain(positiveNumberSchema, 42, \"count\"); // ✓ Valid\n * ascertain(positiveNumberSchema, -5, \"count\"); // ✗ Throws error\n * ```\n */\nexport const and = <T>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T> extends Operator<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\n/**\n * Operator for making a schema optional (nullable).\n *\n * Creates a schema that accepts the provided schema or null/undefined values.\n * This is useful for optional object properties or nullable fields.\n *\n * @template T - The type of data the operator validates.\n * @param schema - The schema to make optional.\n * @returns A schema that validates data against the provided schema or accepts null/undefined.\n *\n * @example\n * ```typescript\n * import { optional, ascertain } from 'ascertain';\n *\n * // Optional string field\n * const userSchema = {\n * name: String,\n * nickname: optional(String),\n * age: Number\n * };\n *\n * // All of these are valid\n * ascertain(userSchema, {\n * name: \"John\",\n * nickname: \"Johnny\",\n * age: 30\n * }, \"user\"); // ✓ Valid\n *\n * ascertain(userSchema, {\n * name: \"Jane\",\n * nickname: null,\n * age: 25\n * }, \"user\"); // ✓ Valid\n *\n * ascertain(userSchema, {\n * name: \"Bob\",\n * age: 35\n * // nickname is undefined\n * }, \"user\"); // ✓ Valid\n *\n * // Optional complex objects\n * const profileSchema = {\n * id: Number,\n * settings: optional({\n * theme: String,\n * notifications: Boolean\n * })\n * };\n * ```\n */\nexport const optional = <T>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T> extends Operator<T> {}\n/**\n * Operator for validating data against a fixed-length tuple of schemas.\n *\n * Creates a schema that validates arrays with a specific length and type for each position.\n * This is useful for coordinate pairs, RGB values, or any fixed-structure data.\n *\n * @template T - The type of data the operator validates (a tuple of types).\n * @param schemas - Schemas for each position in the tuple, in order.\n * @returns A schema that validates data as a tuple with the specified structure.\n *\n * @example\n * ```typescript\n * import { tuple, ascertain } from 'ascertain';\n *\n * // 2D coordinate tuple\n * const pointSchema = tuple(Number, Number);\n * ascertain(pointSchema, [10, 20], \"point\"); // ✓ Valid\n * ascertain(pointSchema, [1.5, 2.7], \"point\"); // ✓ Valid\n * ascertain(pointSchema, [10], \"point\"); // ✗ Throws error (too short)\n * ascertain(pointSchema, [10, 20, 30], \"point\"); // ✗ Throws error (too long)\n *\n * // RGB color tuple\n * const colorSchema = tuple(Number, Number, Number);\n * ascertain(colorSchema, [255, 128, 0], \"color\"); // ✓ Valid\n *\n * // Mixed type tuple\n * const userInfoSchema = tuple(String, Number, Boolean);\n * ascertain(userInfoSchema, [\"Alice\", 25, true], \"userInfo\"); // ✓ Valid\n *\n * // Nested tuple\n * const lineSchema = tuple(\n * tuple(Number, Number), // start point\n * tuple(Number, Number) // end point\n * );\n * ascertain(lineSchema, [[0, 0], [10, 10]], \"line\"); // ✓ Valid\n * ```\n */\nexport const tuple = <T>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const asError = <T>(message: string) => new TypeError(message) as unknown as T;\n\nexport const as = {\n /**\n * Attempts to convert a value to a string.\n *\n * @param value - The value to convert.\n * @returns The value as a string, or a TypeError if not a string.\n */\n string: (value: string | undefined): string => {\n return typeof value === 'string' ? value : asError(`Invalid value \"${value}\", expected a string`);\n },\n /**\n * Attempts to convert a value to a number.\n *\n * @param value - The value to convert (expected to be a string representation of a number).\n * @returns The value as a number, or a TypeError if not a valid number.\n */\n number: (value: string | undefined): number => {\n const result = parseFloat(value as string);\n return Number.isNaN(result) ? asError(`Invalid value ${value}, expected a valid number`) : result;\n },\n /**\n * Attempts to convert a value to a Date object.\n *\n * @param value - The value to convert (expected to be a string representation of a date).\n * @returns The value as a Date object, or a TypeError if not a valid date.\n */\n date: (value: string | undefined): Date => {\n const result = Date.parse(value as string);\n const date = new Date(result);\n return Number.isNaN(date.valueOf()) ? asError(`Invalid value \"${value}\", expected a valid date format`) : date;\n },\n /**\n * Attempts to convert a value to a time duration in milliseconds.\n *\n * @param value - The value to convert (e.g., \"5s\" for 5 seconds).\n * @param conversionFactor - Optional factor to divide the result by (default is 1).\n * @returns The time duration in milliseconds, or a TypeError if the format is invalid.\n */\n time: (value: string | undefined, conversionFactor = 1): number => {\n const matches = value?.match(/^(\\d*\\.?\\d*)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(`${(parseFloat(amount) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS]) / conversionFactor}`);\n }\n return asError(`Invalid value ${value}, expected a valid time format`);\n },\n /**\n * Attempts to convert a value to a boolean.\n *\n * @param value - The boolean like value to convert (e.g., \"true\", \"1\", \"enabled\").\n * @returns The value as a boolean, or a TypeError if it could not be converted to a boolean.\n */\n boolean: (value: string | undefined): boolean =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string)\n ? /^(1|true|enabled)$/i.test(value as string)\n : asError(`Invalid value ${value}, expected a boolean like`),\n /**\n * Attempts to convert a string into an array of strings by splitting it using the given delimiter.\n *\n * @param value - The string value to attempt to split into an array.\n * @param delimiter - The character or string used to separate elements in the input string.\n * @returns An array of strings if the conversion is successful, or a TypeError if the value is not a string.\n */\n array: (value: string | undefined, delimiter: string): string[] => value?.split?.(delimiter) ?? asError(`Invalid value ${value}, expected an array`),\n /**\n * Attempts to parse a JSON string into a JavaScript object.\n *\n * @template T - The expected type of the parsed JSON object.\n * @param value - The JSON string to attempt to parse.\n * @returns The parsed JSON object if successful, or a TypeError if the value is not valid JSON.\n */\n json: <T = object>(value: string | undefined): T => {\n try {\n return JSON.parse(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid JSON string`);\n }\n },\n /**\n * Attempts to decode a base64-encoded string.\n *\n * @param value - The base64-encoded string to attempt to decode.\n * @returns The decoded string if successful, or a TypeError if the value is not valid base64.\n */\n base64: (value: string | undefined): string => {\n try {\n return fromBase64(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid base64 string`);\n }\n },\n};\n\n/**\n * A class representing the context for schema validation.\n *\n * Stores a registry of values encountered during validation and provides methods for managing it.\n * @internal\n */\nclass Context {\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nconst codeGenCollectErrors = (errorsAlias: string, code: string, extra: string = '') => `try {${code}} catch (e) {${errorsAlias}.push(e.message);${extra}}`;\nconst codeGenExpectNoErrors = (errorsAlias: string) => `if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`;\nconst codeGenExpectNonError = (valueAlias: string, path: string) =>\n `if (${valueAlias} instanceof Error) { throw new TypeError(\\`\\${${valueAlias}.message} for path \"${path}\".\\`); }`;\nconst codeGenExpectNonNullable = (valueAlias: string, path: string) =>\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`;\nconst codeGenExpectObject = (valueAlias: string, path: string, instanceOf: string) =>\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of ${instanceOf}\\`); }`;\nconst codeGenExpectArray = (valueAlias: string, path: string) =>\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`;\n\nconst codeGen = <T>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n ${codeGenExpectNoErrors(errorsAlias)}\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight((result, code) => codeGenCollectErrors(errorsAlias, code, result), codeGenExpectNoErrors(errorsAlias));\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n `if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\\`Invalid tuple length \\${${valueAlias}.length} for path \"${path}\", expected ${schema.schemas.length}.\\`); }`,\n ...schema.schemas.map((s, idx) => codeGenCollectErrors(errorsAlias, codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`))),\n codeGenExpectNoErrors(errorsAlias),\n ];\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `const ${registryAlias} = ctx.registry[${index}];`,\n codeGenExpectNonNullable(valueAlias, path),\n ];\n if ((schema as unknown) !== Error && !(schema?.prototype instanceof Error)) {\n code.push(codeGenExpectNonError(valueAlias, path));\n }\n\n code.push(\n `if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected an instance of ${schema?.name}\\`); }`,\n `if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\\`Invalid type \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected type ${schema?.name}\\`); }`,\n `if (Number.isNaN(${valueAlias}?.valueOf?.())) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected a valid ${schema?.name}\\`); }`,\n );\n return code.join('\\n');\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectNonError(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) => `${valueAlias}.forEach((${value},${key}) => { ${codeGenCollectErrors(errorsAlias, codeGen(s, context, value, `${path}[\\${${key}}]`))} });`,\n ),\n );\n\n code.push(codeGenExpectNoErrors(errorsAlias));\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\n${codeGenExpectNonNullable(valueAlias, path)}\n${codeGenExpectNonError(valueAlias, path)}\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected to match ${schema.toString()}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Object'),\n codeGenExpectNonError(valueAlias, path),\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('k');\n const errorsAlias = context.unique('err');\n const kAlias = context.unique('k');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = [];\n${keysAlias}.forEach(${kAlias} => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$keys], context, kAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const kAlias = context.unique('k');\n const entriesAlias = context.unique('en');\n const errorsAlias = context.unique('err');\n code.push(`\nconst ${entriesAlias} = Object.entries(${valueAlias});\nconst ${errorsAlias} = [];\n${entriesAlias}.forEach(([${kAlias},${vAlias}]) => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$values], context, vAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($strict in schema && schema[$strict]) {\n const keysAlias = context.unique('k');\n const kAlias = context.unique('k');\n const extraAlias = context.unique('ex');\n code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);\n code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);\n code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\\`Extra properties: \\${${extraAlias}}, are not allowed for path \"${path}\"\\`); }`);\n }\n code.push(...Object.entries(schema).map(([key, s]) => codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `${code.join('\\n')}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected symbol\\`); }\nif (${valueAlias} !== ${registryAlias}) { throw new TypeError(\\`Invalid value \\${${valueAlias}.toString()} for path \"${path}\", expected ${schema.toString()}\\`); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\\`Invalid value \\${JSON.stringify(${valueAlias})} for path \"${path}\", expected nullable\\`); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${value} = ${JSON.stringify(schema)};\n${codeGenExpectNonError(valueAlias, path)}\nif (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected ${typeof schema}\\`); }\nif (${valueAlias} !== ${value}) { throw new TypeError(\\`Invalid value \\${JSON.stringify(${valueAlias})} for path \"${path}\", expected ${JSON.stringify(schema)}\\`); }\n`;\n }\n};\n\n/**\n * Compiles a schema into a validation function.\n *\n * This function takes a schema definition and generates a JavaScript function\n * that can be used to validate data against the schema.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to compile.\n * @param rootName - A name for the root of the data structure (used in error messages).\n * @returns A validation function that takes data as input and throws a TypeError if the data does not conform to the schema.\n *\n * @example\n * ```typescript\n * import { compile, optional, and, or } from 'ascertain';\n *\n * const userSchema = {\n * name: String,\n * age: Number,\n * email: optional(String),\n * role: or('admin', 'user', 'guest')\n * };\n *\n * const validateUser = compile(userSchema, 'User');\n *\n * // Valid data - no error thrown\n * validateUser({\n * name: 'John Doe',\n * age: 30,\n * email: 'john@example.com',\n * role: 'user'\n * });\n *\n * // Invalid data - throws TypeError\n * try {\n * validateUser({\n * name: 123, // Invalid: should be string\n * age: 'thirty' // Invalid: should be number\n * });\n * } catch (error) {\n * console.error(error.message); // Detailed validation errors\n * }\n * ```\n */\nexport const compile = <T>(schema: Schema<T>, rootName: string) => {\n const context = new Context();\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: T) => validator(context, data);\n};\n\n/**\n * Asserts that data conforms to a given schema.\n *\n * This function is a convenient wrapper around `compile`. It compiles the schema\n * and immediately validates the provided data against it.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to validate against.\n * @param data - The data to validate.\n * @param rootName - A name for the root of the data structure (used in error messages, defaults to '[root]').\n * @throws `{TypeError}` If the data does not conform to the schema.\n *\n * @example\n * ```typescript\n * import { ascertain, optional, and, or } from 'ascertain';\n *\n * const userSchema = {\n * name: String,\n * age: Number,\n * email: optional(String),\n * active: Boolean\n * };\n *\n * const userData = {\n * name: 'Alice',\n * age: 25,\n * email: 'alice@example.com',\n * active: true\n * };\n *\n * // Validate data - throws if invalid, otherwise continues silently\n * ascertain(userSchema, userData, 'UserData');\n * console.log('User data is valid!');\n *\n * // Example with invalid data\n * try {\n * ascertain(userSchema, {\n * name: 'Bob',\n * age: 'twenty-five', // Invalid: should be number\n * active: true\n * }, 'UserData');\n * } catch (error) {\n * console.error('Validation failed:', error.message);\n * }\n * ```\n *\n * @example\n * ```typescript\n * // Array validation\n * const numbersSchema = [Number];\n * const numbers = [1, 2, 3, 4, 5];\n *\n * ascertain(numbersSchema, numbers, 'Numbers');\n *\n * // Tuple validation\n * const coordinateSchema = tuple(Number, Number);\n * const point = [10, 20];\n *\n * ascertain(coordinateSchema, point, 'Point');\n * ```\n */\nexport const ascertain = <T>(schema: Schema<T>, data: T, rootName = '[root]') => {\n compile(schema, rootName)(data);\n};\n"],"names":["$keys","$strict","$values","and","as","asError","ascertain","compile","fromBase64","optional","or","tuple","Operator","schemas","length","TypeError","name","Symbol","for","Or","And","Optional","schema","Tuple","Buffer","value","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","message","string","number","result","parseFloat","Number","isNaN","date","Date","parse","valueOf","time","conversionFactor","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","JSON","base64","Context","registry","varIndex","register","includes","push","indexOf","unique","prefix","codeGenCollectErrors","errorsAlias","code","extra","codeGenExpectNoErrors","codeGenExpectNonError","valueAlias","path","codeGenExpectNonNullable","codeGenExpectObject","instanceOf","codeGenExpectArray","codeGen","context","valuePath","map","join","reduceRight","idx","index","registryAlias","Error","prototype","Array","isArray","key","RegExp","keysAlias","kAlias","vAlias","entriesAlias","extraAlias","stringify","Object","keys","entries","undefined","rootName","validator","Function","data"],"mappings":";;;;;;;;;;;QAsBaA;eAAAA;;QAQAC;eAAAA;;QAJAC;eAAAA;;QAuFAC;eAAAA;;QAgHAC;eAAAA;;QAFAC;eAAAA;;QAqZAC;eAAAA;;QApEAC;eAAAA;;QA5VAC;eAAAA;;QA1CAC;eAAAA;;QA5FAC;eAAAA;;QAoIAC;eAAAA;;;AAzMb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C9C,OAAO,CAACsC,OAAYF,UAAUzB,SAAS2B;AACzC;AA+DO,MAAMzG,YAAY,CAAIgB,QAAmByF,MAASH,WAAW,QAAQ;IAC1ErG,QAAQe,QAAQsF,UAAUG;AAC5B"}
|
package/build/index.d.ts
CHANGED
|
@@ -44,7 +44,33 @@ declare class Or<T> extends Operator<T> {
|
|
|
44
44
|
/**
|
|
45
45
|
* Operator for validating data against any of the provided schemas (logical OR).
|
|
46
46
|
*
|
|
47
|
+
* Creates a schema that accepts data matching any one of the provided schemas.
|
|
48
|
+
* This is useful for creating union types or alternative validation paths.
|
|
49
|
+
*
|
|
47
50
|
* @template T - The type of data the operator validates.
|
|
51
|
+
* @param schemas - Multiple schemas where at least one must match the data.
|
|
52
|
+
* @returns A schema that validates data against any of the provided schemas.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import { or, ascertain } from 'ascertain';
|
|
57
|
+
*
|
|
58
|
+
* // Create a schema that accepts either a string or number
|
|
59
|
+
* const stringOrNumber = or(String, Number);
|
|
60
|
+
*
|
|
61
|
+
* ascertain(stringOrNumber, "hello", "value"); // ✓ Valid
|
|
62
|
+
* ascertain(stringOrNumber, 42, "value"); // ✓ Valid
|
|
63
|
+
* ascertain(stringOrNumber, true, "value"); // ✗ Throws error
|
|
64
|
+
*
|
|
65
|
+
* // Union of literal values
|
|
66
|
+
* const statusSchema = or('pending', 'completed', 'failed');
|
|
67
|
+
* ascertain(statusSchema, 'pending', "status"); // ✓ Valid
|
|
68
|
+
*
|
|
69
|
+
* // Complex schema combinations
|
|
70
|
+
* const userIdSchema = or(Number, { id: Number, temp: Boolean });
|
|
71
|
+
* ascertain(userIdSchema, 123, "userId"); // ✓ Valid
|
|
72
|
+
* ascertain(userIdSchema, { id: 456, temp: true }, "userId"); // ✓ Valid
|
|
73
|
+
* ```
|
|
48
74
|
*/
|
|
49
75
|
export declare const or: <T>(...schemas: Schema<T>[]) => Or<T>;
|
|
50
76
|
declare class And<T> extends Operator<T> {
|
|
@@ -52,7 +78,34 @@ declare class And<T> extends Operator<T> {
|
|
|
52
78
|
/**
|
|
53
79
|
* Operator for validating data against all provided schemas (logical AND).
|
|
54
80
|
*
|
|
81
|
+
* Creates a schema that requires data to match every one of the provided schemas.
|
|
82
|
+
* This is useful for combining multiple validation requirements or adding constraints.
|
|
83
|
+
*
|
|
55
84
|
* @template T - The type of data the operator validates.
|
|
85
|
+
* @param schemas - Multiple schemas that all must match the data.
|
|
86
|
+
* @returns A schema that validates data against all of the provided schemas.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* import { and, ascertain } from 'ascertain';
|
|
91
|
+
*
|
|
92
|
+
* // Combine object schema with additional constraints
|
|
93
|
+
* const userSchema = and(
|
|
94
|
+
* { name: String, age: Number },
|
|
95
|
+
* { age: Number } // Additional constraint
|
|
96
|
+
* );
|
|
97
|
+
*
|
|
98
|
+
* ascertain(userSchema, { name: "John", age: 25 }, "user"); // ✓ Valid
|
|
99
|
+
*
|
|
100
|
+
* // Ensure an object is both a Date and has specific methods
|
|
101
|
+
* const validDateSchema = and(Date, { toISOString: Function });
|
|
102
|
+
* ascertain(validDateSchema, new Date(), "date"); // ✓ Valid
|
|
103
|
+
*
|
|
104
|
+
* // Multiple validation layers
|
|
105
|
+
* const positiveNumberSchema = and(Number, (n: number) => n > 0);
|
|
106
|
+
* ascertain(positiveNumberSchema, 42, "count"); // ✓ Valid
|
|
107
|
+
* ascertain(positiveNumberSchema, -5, "count"); // ✗ Throws error
|
|
108
|
+
* ```
|
|
56
109
|
*/
|
|
57
110
|
export declare const and: <T>(...schemas: Schema<T>[]) => And<T>;
|
|
58
111
|
declare class Optional<T> extends Operator<T> {
|
|
@@ -61,7 +114,52 @@ declare class Optional<T> extends Operator<T> {
|
|
|
61
114
|
/**
|
|
62
115
|
* Operator for making a schema optional (nullable).
|
|
63
116
|
*
|
|
117
|
+
* Creates a schema that accepts the provided schema or null/undefined values.
|
|
118
|
+
* This is useful for optional object properties or nullable fields.
|
|
119
|
+
*
|
|
64
120
|
* @template T - The type of data the operator validates.
|
|
121
|
+
* @param schema - The schema to make optional.
|
|
122
|
+
* @returns A schema that validates data against the provided schema or accepts null/undefined.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* import { optional, ascertain } from 'ascertain';
|
|
127
|
+
*
|
|
128
|
+
* // Optional string field
|
|
129
|
+
* const userSchema = {
|
|
130
|
+
* name: String,
|
|
131
|
+
* nickname: optional(String),
|
|
132
|
+
* age: Number
|
|
133
|
+
* };
|
|
134
|
+
*
|
|
135
|
+
* // All of these are valid
|
|
136
|
+
* ascertain(userSchema, {
|
|
137
|
+
* name: "John",
|
|
138
|
+
* nickname: "Johnny",
|
|
139
|
+
* age: 30
|
|
140
|
+
* }, "user"); // ✓ Valid
|
|
141
|
+
*
|
|
142
|
+
* ascertain(userSchema, {
|
|
143
|
+
* name: "Jane",
|
|
144
|
+
* nickname: null,
|
|
145
|
+
* age: 25
|
|
146
|
+
* }, "user"); // ✓ Valid
|
|
147
|
+
*
|
|
148
|
+
* ascertain(userSchema, {
|
|
149
|
+
* name: "Bob",
|
|
150
|
+
* age: 35
|
|
151
|
+
* // nickname is undefined
|
|
152
|
+
* }, "user"); // ✓ Valid
|
|
153
|
+
*
|
|
154
|
+
* // Optional complex objects
|
|
155
|
+
* const profileSchema = {
|
|
156
|
+
* id: Number,
|
|
157
|
+
* settings: optional({
|
|
158
|
+
* theme: String,
|
|
159
|
+
* notifications: Boolean
|
|
160
|
+
* })
|
|
161
|
+
* };
|
|
162
|
+
* ```
|
|
65
163
|
*/
|
|
66
164
|
export declare const optional: <T>(schema: Schema<T>) => Optional<T>;
|
|
67
165
|
declare class Tuple<T> extends Operator<T> {
|
|
@@ -69,7 +167,39 @@ declare class Tuple<T> extends Operator<T> {
|
|
|
69
167
|
/**
|
|
70
168
|
* Operator for validating data against a fixed-length tuple of schemas.
|
|
71
169
|
*
|
|
170
|
+
* Creates a schema that validates arrays with a specific length and type for each position.
|
|
171
|
+
* This is useful for coordinate pairs, RGB values, or any fixed-structure data.
|
|
172
|
+
*
|
|
72
173
|
* @template T - The type of data the operator validates (a tuple of types).
|
|
174
|
+
* @param schemas - Schemas for each position in the tuple, in order.
|
|
175
|
+
* @returns A schema that validates data as a tuple with the specified structure.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* import { tuple, ascertain } from 'ascertain';
|
|
180
|
+
*
|
|
181
|
+
* // 2D coordinate tuple
|
|
182
|
+
* const pointSchema = tuple(Number, Number);
|
|
183
|
+
* ascertain(pointSchema, [10, 20], "point"); // ✓ Valid
|
|
184
|
+
* ascertain(pointSchema, [1.5, 2.7], "point"); // ✓ Valid
|
|
185
|
+
* ascertain(pointSchema, [10], "point"); // ✗ Throws error (too short)
|
|
186
|
+
* ascertain(pointSchema, [10, 20, 30], "point"); // ✗ Throws error (too long)
|
|
187
|
+
*
|
|
188
|
+
* // RGB color tuple
|
|
189
|
+
* const colorSchema = tuple(Number, Number, Number);
|
|
190
|
+
* ascertain(colorSchema, [255, 128, 0], "color"); // ✓ Valid
|
|
191
|
+
*
|
|
192
|
+
* // Mixed type tuple
|
|
193
|
+
* const userInfoSchema = tuple(String, Number, Boolean);
|
|
194
|
+
* ascertain(userInfoSchema, ["Alice", 25, true], "userInfo"); // ✓ Valid
|
|
195
|
+
*
|
|
196
|
+
* // Nested tuple
|
|
197
|
+
* const lineSchema = tuple(
|
|
198
|
+
* tuple(Number, Number), // start point
|
|
199
|
+
* tuple(Number, Number) // end point
|
|
200
|
+
* );
|
|
201
|
+
* ascertain(lineSchema, [[0, 0], [10, 10]], "line"); // ✓ Valid
|
|
202
|
+
* ```
|
|
73
203
|
*/
|
|
74
204
|
export declare const tuple: <T>(...schemas: Schema<T>[]) => Tuple<T>;
|
|
75
205
|
export declare const fromBase64: (value: string) => string;
|
|
@@ -93,7 +223,7 @@ export declare const as: {
|
|
|
93
223
|
* Attempts to convert a value to a Date object.
|
|
94
224
|
*
|
|
95
225
|
* @param value - The value to convert (expected to be a string representation of a date).
|
|
96
|
-
* @returns The value as a Date object, or a TypeError if
|
|
226
|
+
* @returns The value as a Date object, or a TypeError if not a valid date.
|
|
97
227
|
*/
|
|
98
228
|
date: (value: string | undefined) => Date;
|
|
99
229
|
/**
|
|
@@ -145,6 +275,38 @@ export declare const as: {
|
|
|
145
275
|
* @param schema - The schema to compile.
|
|
146
276
|
* @param rootName - A name for the root of the data structure (used in error messages).
|
|
147
277
|
* @returns A validation function that takes data as input and throws a TypeError if the data does not conform to the schema.
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```typescript
|
|
281
|
+
* import { compile, optional, and, or } from 'ascertain';
|
|
282
|
+
*
|
|
283
|
+
* const userSchema = {
|
|
284
|
+
* name: String,
|
|
285
|
+
* age: Number,
|
|
286
|
+
* email: optional(String),
|
|
287
|
+
* role: or('admin', 'user', 'guest')
|
|
288
|
+
* };
|
|
289
|
+
*
|
|
290
|
+
* const validateUser = compile(userSchema, 'User');
|
|
291
|
+
*
|
|
292
|
+
* // Valid data - no error thrown
|
|
293
|
+
* validateUser({
|
|
294
|
+
* name: 'John Doe',
|
|
295
|
+
* age: 30,
|
|
296
|
+
* email: 'john@example.com',
|
|
297
|
+
* role: 'user'
|
|
298
|
+
* });
|
|
299
|
+
*
|
|
300
|
+
* // Invalid data - throws TypeError
|
|
301
|
+
* try {
|
|
302
|
+
* validateUser({
|
|
303
|
+
* name: 123, // Invalid: should be string
|
|
304
|
+
* age: 'thirty' // Invalid: should be number
|
|
305
|
+
* });
|
|
306
|
+
* } catch (error) {
|
|
307
|
+
* console.error(error.message); // Detailed validation errors
|
|
308
|
+
* }
|
|
309
|
+
* ```
|
|
148
310
|
*/
|
|
149
311
|
export declare const compile: <T>(schema: Schema<T>, rootName: string) => (data: T) => any;
|
|
150
312
|
/**
|
|
@@ -157,7 +319,56 @@ export declare const compile: <T>(schema: Schema<T>, rootName: string) => (data:
|
|
|
157
319
|
* @param schema - The schema to validate against.
|
|
158
320
|
* @param data - The data to validate.
|
|
159
321
|
* @param rootName - A name for the root of the data structure (used in error messages, defaults to '[root]').
|
|
160
|
-
* @throws {TypeError} If the data does not conform to the schema.
|
|
322
|
+
* @throws `{TypeError}` If the data does not conform to the schema.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```typescript
|
|
326
|
+
* import { ascertain, optional, and, or } from 'ascertain';
|
|
327
|
+
*
|
|
328
|
+
* const userSchema = {
|
|
329
|
+
* name: String,
|
|
330
|
+
* age: Number,
|
|
331
|
+
* email: optional(String),
|
|
332
|
+
* active: Boolean
|
|
333
|
+
* };
|
|
334
|
+
*
|
|
335
|
+
* const userData = {
|
|
336
|
+
* name: 'Alice',
|
|
337
|
+
* age: 25,
|
|
338
|
+
* email: 'alice@example.com',
|
|
339
|
+
* active: true
|
|
340
|
+
* };
|
|
341
|
+
*
|
|
342
|
+
* // Validate data - throws if invalid, otherwise continues silently
|
|
343
|
+
* ascertain(userSchema, userData, 'UserData');
|
|
344
|
+
* console.log('User data is valid!');
|
|
345
|
+
*
|
|
346
|
+
* // Example with invalid data
|
|
347
|
+
* try {
|
|
348
|
+
* ascertain(userSchema, {
|
|
349
|
+
* name: 'Bob',
|
|
350
|
+
* age: 'twenty-five', // Invalid: should be number
|
|
351
|
+
* active: true
|
|
352
|
+
* }, 'UserData');
|
|
353
|
+
* } catch (error) {
|
|
354
|
+
* console.error('Validation failed:', error.message);
|
|
355
|
+
* }
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```typescript
|
|
360
|
+
* // Array validation
|
|
361
|
+
* const numbersSchema = [Number];
|
|
362
|
+
* const numbers = [1, 2, 3, 4, 5];
|
|
363
|
+
*
|
|
364
|
+
* ascertain(numbersSchema, numbers, 'Numbers');
|
|
365
|
+
*
|
|
366
|
+
* // Tuple validation
|
|
367
|
+
* const coordinateSchema = tuple(Number, Number);
|
|
368
|
+
* const point = [10, 20];
|
|
369
|
+
*
|
|
370
|
+
* ascertain(coordinateSchema, point, 'Point');
|
|
371
|
+
* ```
|
|
161
372
|
*/
|
|
162
373
|
export declare const ascertain: <T>(schema: Schema<T>, data: T, rootName?: string) => void;
|
|
163
374
|
export {};
|
package/build/index.js
CHANGED
|
@@ -225,14 +225,14 @@ ${codeGenExpectNoErrors(errorsAlias)}
|
|
|
225
225
|
return `
|
|
226
226
|
const ${valueAlias} = ${valuePath};
|
|
227
227
|
const ${registryAlias} = ctx.registry[${index}];
|
|
228
|
-
if (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for "path", expected symbol\`); }
|
|
228
|
+
if (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\`Invalid type \${typeof ${valueAlias}} for path "${path}", expected symbol\`); }
|
|
229
229
|
if (${valueAlias} !== ${registryAlias}) { throw new TypeError(\`Invalid value \${${valueAlias}.toString()} for path "${path}", expected ${schema.toString()}\`); }
|
|
230
230
|
`;
|
|
231
231
|
} else if (schema === null || schema === undefined) {
|
|
232
232
|
const valueAlias = context.unique('v');
|
|
233
233
|
return `
|
|
234
234
|
const ${valueAlias} = ${valuePath};
|
|
235
|
-
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\`Invalid value ${valueAlias} for path "${path}", expected nullable\`); }
|
|
235
|
+
if (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\`Invalid value \${JSON.stringify(${valueAlias})} for path "${path}", expected nullable\`); }
|
|
236
236
|
`;
|
|
237
237
|
} else {
|
|
238
238
|
const valueAlias = context.unique('v');
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Abstract base class for schema operators.\n *\n * Provides a common constructor that enforces having at least one schema.\n *\n * @template T - The type of data the operator validates.\n * @abstract\n * @internal\n */\nabstract class Operator<T> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\n// https://standardschema.dev/\n\n/**\n * Symbol for validating object keys against a schema.\n */\nexport const $keys = Symbol.for('@@keys');\n/**\n * Symbol for validating object values against a schema.\n */\nexport const $values = Symbol.for('@@values');\n/**\n * Symbol for enforcing strict object validation (no extra properties allowed).\n */\nexport const $strict = Symbol.for('@@strict');\n\n/**\n * Represents a schema for validating data.\n *\n * Schemas can be defined for various data types, including objects, arrays, and primitives.\n *\n * @template T - The type of data the schema validates.\n */\nexport type Schema<T> =\n T extends Record<string | number | symbol, unknown>\n ? { [K in keyof T]?: Schema<T[K]> | unknown } & { [$keys]?: Schema<keyof T> } & { [$values]?: Schema<T[keyof T]> } & { [$strict]?: boolean }\n : T extends Array<infer A>\n ? Schema<A>[] | unknown\n : unknown;\n\nclass Or<T> extends Operator<T> {}\n/**\n * Operator for validating data against any of the provided schemas (logical OR).\n *\n * @template T - The type of data the operator validates.\n */\nexport const or = <T>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T> extends Operator<T> {}\n/**\n * Operator for validating data against all provided schemas (logical AND).\n *\n * @template T - The type of data the operator validates.\n */\nexport const and = <T>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T> extends Operator<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\n/**\n * Operator for making a schema optional (nullable).\n *\n * @template T - The type of data the operator validates.\n */\nexport const optional = <T>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T> extends Operator<T> {}\n/**\n * Operator for validating data against a fixed-length tuple of schemas.\n *\n * @template T - The type of data the operator validates (a tuple of types).\n */\nexport const tuple = <T>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const asError = <T>(message: string) => new TypeError(message) as unknown as T;\n\nexport const as = {\n /**\n * Attempts to convert a value to a string.\n *\n * @param value - The value to convert.\n * @returns The value as a string, or a TypeError if not a string.\n */\n string: (value: string | undefined): string => {\n return typeof value === 'string' ? value : asError(`Invalid value \"${value}\", expected a string`);\n },\n /**\n * Attempts to convert a value to a number.\n *\n * @param value - The value to convert (expected to be a string representation of a number).\n * @returns The value as a number, or a TypeError if not a valid number.\n */\n number: (value: string | undefined): number => {\n const result = parseFloat(value as string);\n return Number.isNaN(result) ? asError(`Invalid value ${value}, expected a valid number`) : result;\n },\n /**\n * Attempts to convert a value to a Date object.\n *\n * @param value - The value to convert (expected to be a string representation of a date).\n * @returns The value as a Date object, or a TypeError if no a valid date.\n */\n date: (value: string | undefined): Date => {\n const result = Date.parse(value as string);\n const date = new Date(result);\n return Number.isNaN(date.valueOf()) ? asError(`Invalid value \"${value}\", expected a valid date format`) : date;\n },\n /**\n * Attempts to convert a value to a time duration in milliseconds.\n *\n * @param value - The value to convert (e.g., \"5s\" for 5 seconds).\n * @param conversionFactor - Optional factor to divide the result by (default is 1).\n * @returns The time duration in milliseconds, or a TypeError if the format is invalid.\n */\n time: (value: string | undefined, conversionFactor = 1): number => {\n const matches = value?.match(/^(\\d*\\.?\\d*)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(`${(parseFloat(amount) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS]) / conversionFactor}`);\n }\n return asError(`Invalid value ${value}, expected a valid time format`);\n },\n /**\n * Attempts to convert a value to a boolean.\n *\n * @param value - The boolean like value to convert (e.g., \"true\", \"1\", \"enabled\").\n * @returns The value as a boolean, or a TypeError if it could not be converted to a boolean.\n */\n boolean: (value: string | undefined): boolean =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string)\n ? /^(1|true|enabled)$/i.test(value as string)\n : asError(`Invalid value ${value}, expected a boolean like`),\n /**\n * Attempts to convert a string into an array of strings by splitting it using the given delimiter.\n *\n * @param value - The string value to attempt to split into an array.\n * @param delimiter - The character or string used to separate elements in the input string.\n * @returns An array of strings if the conversion is successful, or a TypeError if the value is not a string.\n */\n array: (value: string | undefined, delimiter: string): string[] => value?.split?.(delimiter) ?? asError(`Invalid value ${value}, expected an array`),\n /**\n * Attempts to parse a JSON string into a JavaScript object.\n *\n * @template T - The expected type of the parsed JSON object.\n * @param value - The JSON string to attempt to parse.\n * @returns The parsed JSON object if successful, or a TypeError if the value is not valid JSON.\n */\n json: <T = object>(value: string | undefined): T => {\n try {\n return JSON.parse(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid JSON string`);\n }\n },\n /**\n * Attempts to decode a base64-encoded string.\n *\n * @param value - The base64-encoded string to attempt to decode.\n * @returns The decoded string if successful, or a TypeError if the value is not valid base64.\n */\n base64: (value: string | undefined): string => {\n try {\n return fromBase64(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid base64 string`);\n }\n },\n};\n\n/**\n * A class representing the context for schema validation.\n *\n * Stores a registry of values encountered during validation and provides methods for managing it.\n * @internal\n */\nclass Context {\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nconst codeGenCollectErrors = (errorsAlias: string, code: string, extra: string = '') => `try {${code}} catch (e) {${errorsAlias}.push(e.message);${extra}}`;\nconst codeGenExpectNoErrors = (errorsAlias: string) => `if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`;\nconst codeGenExpectNonError = (valueAlias: string, path: string) =>\n `if (${valueAlias} instanceof Error) { throw new TypeError(\\`\\${${valueAlias}.message} for path \"${path}\".\\`); }`;\nconst codeGenExpectNonNullable = (valueAlias: string, path: string) =>\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`;\nconst codeGenExpectObject = (valueAlias: string, path: string, instanceOf: string) =>\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of ${instanceOf}\\`); }`;\nconst codeGenExpectArray = (valueAlias: string, path: string) =>\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`;\n\nconst codeGen = <T>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n ${codeGenExpectNoErrors(errorsAlias)}\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight((result, code) => codeGenCollectErrors(errorsAlias, code, result), codeGenExpectNoErrors(errorsAlias));\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n `if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\\`Invalid tuple length \\${${valueAlias}.length} for path \"${path}\", expected ${schema.schemas.length}.\\`); }`,\n ...schema.schemas.map((s, idx) => codeGenCollectErrors(errorsAlias, codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`))),\n codeGenExpectNoErrors(errorsAlias),\n ];\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `const ${registryAlias} = ctx.registry[${index}];`,\n codeGenExpectNonNullable(valueAlias, path),\n ];\n if ((schema as unknown) !== Error && !(schema?.prototype instanceof Error)) {\n code.push(codeGenExpectNonError(valueAlias, path));\n }\n\n code.push(\n `if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected an instance of ${schema?.name}\\`); }`,\n `if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\\`Invalid type \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected type ${schema?.name}\\`); }`,\n `if (Number.isNaN(${valueAlias}?.valueOf?.())) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected a valid ${schema?.name}\\`); }`,\n );\n return code.join('\\n');\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectNonError(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) => `${valueAlias}.forEach((${value},${key}) => { ${codeGenCollectErrors(errorsAlias, codeGen(s, context, value, `${path}[\\${${key}}]`))} });`,\n ),\n );\n\n code.push(codeGenExpectNoErrors(errorsAlias));\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\n${codeGenExpectNonNullable(valueAlias, path)}\n${codeGenExpectNonError(valueAlias, path)}\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected to match ${schema.toString()}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Object'),\n codeGenExpectNonError(valueAlias, path),\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('k');\n const errorsAlias = context.unique('err');\n const kAlias = context.unique('k');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = [];\n${keysAlias}.forEach(${kAlias} => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$keys], context, kAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const kAlias = context.unique('k');\n const entriesAlias = context.unique('en');\n const errorsAlias = context.unique('err');\n code.push(`\nconst ${entriesAlias} = Object.entries(${valueAlias});\nconst ${errorsAlias} = [];\n${entriesAlias}.forEach(([${kAlias},${vAlias}]) => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$values], context, vAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($strict in schema && schema[$strict]) {\n const keysAlias = context.unique('k');\n const kAlias = context.unique('k');\n const extraAlias = context.unique('ex');\n code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);\n code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);\n code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\\`Extra properties: \\${${extraAlias}}, are not allowed for path \"${path}\"\\`); }`);\n }\n code.push(...Object.entries(schema).map(([key, s]) => codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `${code.join('\\n')}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for \"path\", expected symbol\\`); }\nif (${valueAlias} !== ${registryAlias}) { throw new TypeError(\\`Invalid value \\${${valueAlias}.toString()} for path \"${path}\", expected ${schema.toString()}\\`); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\\`Invalid value ${valueAlias} for path \"${path}\", expected nullable\\`); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${value} = ${JSON.stringify(schema)};\n${codeGenExpectNonError(valueAlias, path)}\nif (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected ${typeof schema}\\`); }\nif (${valueAlias} !== ${value}) { throw new TypeError(\\`Invalid value \\${JSON.stringify(${valueAlias})} for path \"${path}\", expected ${JSON.stringify(schema)}\\`); }\n`;\n }\n};\n\n/**\n * Compiles a schema into a validation function.\n *\n * This function takes a schema definition and generates a JavaScript function\n * that can be used to validate data against the schema.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to compile.\n * @param rootName - A name for the root of the data structure (used in error messages).\n * @returns A validation function that takes data as input and throws a TypeError if the data does not conform to the schema.\n */\nexport const compile = <T>(schema: Schema<T>, rootName: string) => {\n const context = new Context();\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: T) => validator(context, data);\n};\n\n/**\n * Asserts that data conforms to a given schema.\n *\n * This function is a convenient wrapper around `compile`. It compiles the schema\n * and immediately validates the provided data against it.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to validate against.\n * @param data - The data to validate.\n * @param rootName - A name for the root of the data structure (used in error messages, defaults to '[root]').\n * @throws {TypeError} If the data does not conform to the schema.\n */\nexport const ascertain = <T>(schema: Schema<T>, data: T, rootName = '[root]') => {\n compile(schema, rootName)(data);\n};\n"],"names":["Operator","schemas","length","TypeError","name","$keys","Symbol","for","$values","$strict","Or","or","And","and","Optional","schema","optional","Tuple","tuple","fromBase64","Buffer","value","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","asError","message","as","string","number","result","parseFloat","Number","isNaN","date","Date","parse","valueOf","time","conversionFactor","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","JSON","base64","Context","registry","varIndex","register","includes","push","indexOf","unique","prefix","codeGenCollectErrors","errorsAlias","code","extra","codeGenExpectNoErrors","codeGenExpectNonError","valueAlias","path","codeGenExpectNonNullable","codeGenExpectObject","instanceOf","codeGenExpectArray","codeGen","context","valuePath","map","join","reduceRight","idx","index","registryAlias","Error","prototype","Array","isArray","key","RegExp","keysAlias","kAlias","vAlias","entriesAlias","extraAlias","stringify","Object","keys","entries","undefined","compile","rootName","validator","Function","data","ascertain"],"mappings":"AASA,MAAeA;;IACb,YAAY,AAAgBC,OAAoB,CAAE;aAAtBA,UAAAA;QAC1B,IAAIA,QAAQC,MAAM,KAAK,GAAG;YACxB,MAAM,IAAIC,UAAU,CAAC,iBAAiB,EAAE,IAAI,CAAC,WAAW,CAACC,IAAI,CAAC,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MAASH,WAAW,QAAQ;IAC1ED,QAAQ3F,QAAQ4F,UAAUG;AAC5B,EAAE"}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Abstract base class for schema operators.\n *\n * Provides a common constructor that enforces having at least one schema.\n *\n * @template T - The type of data the operator validates.\n * @abstract\n * @internal\n */\nabstract class Operator<T> {\n constructor(public readonly schemas: Schema<T>[]) {\n if (schemas.length === 0) {\n throw new TypeError(`Operation schema ${this.constructor.name} must have at least one element`);\n }\n }\n}\n\n// https://standardschema.dev/\n\n/**\n * Symbol for validating object keys against a schema.\n */\nexport const $keys = Symbol.for('@@keys');\n/**\n * Symbol for validating object values against a schema.\n */\nexport const $values = Symbol.for('@@values');\n/**\n * Symbol for enforcing strict object validation (no extra properties allowed).\n */\nexport const $strict = Symbol.for('@@strict');\n\n/**\n * Represents a schema for validating data.\n *\n * Schemas can be defined for various data types, including objects, arrays, and primitives.\n *\n * @template T - The type of data the schema validates.\n */\nexport type Schema<T> =\n T extends Record<string | number | symbol, unknown>\n ? { [K in keyof T]?: Schema<T[K]> | unknown } & { [$keys]?: Schema<keyof T> } & { [$values]?: Schema<T[keyof T]> } & { [$strict]?: boolean }\n : T extends Array<infer A>\n ? Schema<A>[] | unknown\n : unknown;\n\nclass Or<T> extends Operator<T> {}\n/**\n * Operator for validating data against any of the provided schemas (logical OR).\n *\n * Creates a schema that accepts data matching any one of the provided schemas.\n * This is useful for creating union types or alternative validation paths.\n *\n * @template T - The type of data the operator validates.\n * @param schemas - Multiple schemas where at least one must match the data.\n * @returns A schema that validates data against any of the provided schemas.\n *\n * @example\n * ```typescript\n * import { or, ascertain } from 'ascertain';\n *\n * // Create a schema that accepts either a string or number\n * const stringOrNumber = or(String, Number);\n *\n * ascertain(stringOrNumber, \"hello\", \"value\"); // ✓ Valid\n * ascertain(stringOrNumber, 42, \"value\"); // ✓ Valid\n * ascertain(stringOrNumber, true, \"value\"); // ✗ Throws error\n *\n * // Union of literal values\n * const statusSchema = or('pending', 'completed', 'failed');\n * ascertain(statusSchema, 'pending', \"status\"); // ✓ Valid\n *\n * // Complex schema combinations\n * const userIdSchema = or(Number, { id: Number, temp: Boolean });\n * ascertain(userIdSchema, 123, \"userId\"); // ✓ Valid\n * ascertain(userIdSchema, { id: 456, temp: true }, \"userId\"); // ✓ Valid\n * ```\n */\nexport const or = <T>(...schemas: Schema<T>[]) => new Or(schemas);\n\nclass And<T> extends Operator<T> {}\n/**\n * Operator for validating data against all provided schemas (logical AND).\n *\n * Creates a schema that requires data to match every one of the provided schemas.\n * This is useful for combining multiple validation requirements or adding constraints.\n *\n * @template T - The type of data the operator validates.\n * @param schemas - Multiple schemas that all must match the data.\n * @returns A schema that validates data against all of the provided schemas.\n *\n * @example\n * ```typescript\n * import { and, ascertain } from 'ascertain';\n *\n * // Combine object schema with additional constraints\n * const userSchema = and(\n * { name: String, age: Number },\n * { age: Number } // Additional constraint\n * );\n *\n * ascertain(userSchema, { name: \"John\", age: 25 }, \"user\"); // ✓ Valid\n *\n * // Ensure an object is both a Date and has specific methods\n * const validDateSchema = and(Date, { toISOString: Function });\n * ascertain(validDateSchema, new Date(), \"date\"); // ✓ Valid\n *\n * // Multiple validation layers\n * const positiveNumberSchema = and(Number, (n: number) => n > 0);\n * ascertain(positiveNumberSchema, 42, \"count\"); // ✓ Valid\n * ascertain(positiveNumberSchema, -5, \"count\"); // ✗ Throws error\n * ```\n */\nexport const and = <T>(...schemas: Schema<T>[]) => new And(schemas);\n\nclass Optional<T> extends Operator<T> {\n constructor(schema: Schema<T>) {\n super([schema]);\n }\n}\n/**\n * Operator for making a schema optional (nullable).\n *\n * Creates a schema that accepts the provided schema or null/undefined values.\n * This is useful for optional object properties or nullable fields.\n *\n * @template T - The type of data the operator validates.\n * @param schema - The schema to make optional.\n * @returns A schema that validates data against the provided schema or accepts null/undefined.\n *\n * @example\n * ```typescript\n * import { optional, ascertain } from 'ascertain';\n *\n * // Optional string field\n * const userSchema = {\n * name: String,\n * nickname: optional(String),\n * age: Number\n * };\n *\n * // All of these are valid\n * ascertain(userSchema, {\n * name: \"John\",\n * nickname: \"Johnny\",\n * age: 30\n * }, \"user\"); // ✓ Valid\n *\n * ascertain(userSchema, {\n * name: \"Jane\",\n * nickname: null,\n * age: 25\n * }, \"user\"); // ✓ Valid\n *\n * ascertain(userSchema, {\n * name: \"Bob\",\n * age: 35\n * // nickname is undefined\n * }, \"user\"); // ✓ Valid\n *\n * // Optional complex objects\n * const profileSchema = {\n * id: Number,\n * settings: optional({\n * theme: String,\n * notifications: Boolean\n * })\n * };\n * ```\n */\nexport const optional = <T>(schema: Schema<T>) => new Optional(schema);\n\nclass Tuple<T> extends Operator<T> {}\n/**\n * Operator for validating data against a fixed-length tuple of schemas.\n *\n * Creates a schema that validates arrays with a specific length and type for each position.\n * This is useful for coordinate pairs, RGB values, or any fixed-structure data.\n *\n * @template T - The type of data the operator validates (a tuple of types).\n * @param schemas - Schemas for each position in the tuple, in order.\n * @returns A schema that validates data as a tuple with the specified structure.\n *\n * @example\n * ```typescript\n * import { tuple, ascertain } from 'ascertain';\n *\n * // 2D coordinate tuple\n * const pointSchema = tuple(Number, Number);\n * ascertain(pointSchema, [10, 20], \"point\"); // ✓ Valid\n * ascertain(pointSchema, [1.5, 2.7], \"point\"); // ✓ Valid\n * ascertain(pointSchema, [10], \"point\"); // ✗ Throws error (too short)\n * ascertain(pointSchema, [10, 20, 30], \"point\"); // ✗ Throws error (too long)\n *\n * // RGB color tuple\n * const colorSchema = tuple(Number, Number, Number);\n * ascertain(colorSchema, [255, 128, 0], \"color\"); // ✓ Valid\n *\n * // Mixed type tuple\n * const userInfoSchema = tuple(String, Number, Boolean);\n * ascertain(userInfoSchema, [\"Alice\", 25, true], \"userInfo\"); // ✓ Valid\n *\n * // Nested tuple\n * const lineSchema = tuple(\n * tuple(Number, Number), // start point\n * tuple(Number, Number) // end point\n * );\n * ascertain(lineSchema, [[0, 0], [10, 10]], \"line\"); // ✓ Valid\n * ```\n */\nexport const tuple = <T>(...schemas: Schema<T>[]) => new Tuple(schemas);\n\nexport const fromBase64 = typeof Buffer === 'undefined' ? (value: string) => atob(value) : (value: string) => Buffer.from(value, 'base64').toString('utf-8');\n\nconst MULTIPLIERS = {\n ms: 1,\n s: 1000,\n m: 60000,\n h: 3600000,\n d: 86400000,\n w: 604800000,\n};\n\nexport const asError = <T>(message: string) => new TypeError(message) as unknown as T;\n\nexport const as = {\n /**\n * Attempts to convert a value to a string.\n *\n * @param value - The value to convert.\n * @returns The value as a string, or a TypeError if not a string.\n */\n string: (value: string | undefined): string => {\n return typeof value === 'string' ? value : asError(`Invalid value \"${value}\", expected a string`);\n },\n /**\n * Attempts to convert a value to a number.\n *\n * @param value - The value to convert (expected to be a string representation of a number).\n * @returns The value as a number, or a TypeError if not a valid number.\n */\n number: (value: string | undefined): number => {\n const result = parseFloat(value as string);\n return Number.isNaN(result) ? asError(`Invalid value ${value}, expected a valid number`) : result;\n },\n /**\n * Attempts to convert a value to a Date object.\n *\n * @param value - The value to convert (expected to be a string representation of a date).\n * @returns The value as a Date object, or a TypeError if not a valid date.\n */\n date: (value: string | undefined): Date => {\n const result = Date.parse(value as string);\n const date = new Date(result);\n return Number.isNaN(date.valueOf()) ? asError(`Invalid value \"${value}\", expected a valid date format`) : date;\n },\n /**\n * Attempts to convert a value to a time duration in milliseconds.\n *\n * @param value - The value to convert (e.g., \"5s\" for 5 seconds).\n * @param conversionFactor - Optional factor to divide the result by (default is 1).\n * @returns The time duration in milliseconds, or a TypeError if the format is invalid.\n */\n time: (value: string | undefined, conversionFactor = 1): number => {\n const matches = value?.match(/^(\\d*\\.?\\d*)(ms|s|m|h|d|w)?$/);\n if (matches) {\n const [, amount, unit = 'ms'] = matches;\n return parseInt(`${(parseFloat(amount) * MULTIPLIERS[unit as keyof typeof MULTIPLIERS]) / conversionFactor}`);\n }\n return asError(`Invalid value ${value}, expected a valid time format`);\n },\n /**\n * Attempts to convert a value to a boolean.\n *\n * @param value - The boolean like value to convert (e.g., \"true\", \"1\", \"enabled\").\n * @returns The value as a boolean, or a TypeError if it could not be converted to a boolean.\n */\n boolean: (value: string | undefined): boolean =>\n /^(0|1|true|false|enabled|disabled)$/i.test(value as string)\n ? /^(1|true|enabled)$/i.test(value as string)\n : asError(`Invalid value ${value}, expected a boolean like`),\n /**\n * Attempts to convert a string into an array of strings by splitting it using the given delimiter.\n *\n * @param value - The string value to attempt to split into an array.\n * @param delimiter - The character or string used to separate elements in the input string.\n * @returns An array of strings if the conversion is successful, or a TypeError if the value is not a string.\n */\n array: (value: string | undefined, delimiter: string): string[] => value?.split?.(delimiter) ?? asError(`Invalid value ${value}, expected an array`),\n /**\n * Attempts to parse a JSON string into a JavaScript object.\n *\n * @template T - The expected type of the parsed JSON object.\n * @param value - The JSON string to attempt to parse.\n * @returns The parsed JSON object if successful, or a TypeError if the value is not valid JSON.\n */\n json: <T = object>(value: string | undefined): T => {\n try {\n return JSON.parse(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid JSON string`);\n }\n },\n /**\n * Attempts to decode a base64-encoded string.\n *\n * @param value - The base64-encoded string to attempt to decode.\n * @returns The decoded string if successful, or a TypeError if the value is not valid base64.\n */\n base64: (value: string | undefined): string => {\n try {\n return fromBase64(value as string);\n } catch {\n return asError(`Invalid value ${value}, expected a valid base64 string`);\n }\n },\n};\n\n/**\n * A class representing the context for schema validation.\n *\n * Stores a registry of values encountered during validation and provides methods for managing it.\n * @internal\n */\nclass Context {\n public readonly registry: unknown[] = [];\n private varIndex = 0;\n\n register(value: unknown): number {\n if (!this.registry.includes(value)) {\n this.registry.push(value);\n }\n return this.registry.indexOf(value);\n }\n\n unique(prefix: string) {\n return `${prefix}$$${this.varIndex++}`;\n }\n}\n\nconst codeGenCollectErrors = (errorsAlias: string, code: string, extra: string = '') => `try {${code}} catch (e) {${errorsAlias}.push(e.message);${extra}}`;\nconst codeGenExpectNoErrors = (errorsAlias: string) => `if (${errorsAlias}.length !== 0) { throw new TypeError(${errorsAlias}.join('\\\\n')); }`;\nconst codeGenExpectNonError = (valueAlias: string, path: string) =>\n `if (${valueAlias} instanceof Error) { throw new TypeError(\\`\\${${valueAlias}.message} for path \"${path}\".\\`); }`;\nconst codeGenExpectNonNullable = (valueAlias: string, path: string) =>\n `if (${valueAlias} === null || ${valueAlias} === undefined) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected non-nullable.\\`); }`;\nconst codeGenExpectObject = (valueAlias: string, path: string, instanceOf: string) =>\n `if (typeof ${valueAlias} !== 'object') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected an instance of ${instanceOf}\\`); }`;\nconst codeGenExpectArray = (valueAlias: string, path: string) =>\n `if (!Array.isArray(${valueAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}.constructor?.name} for path \"${path}\", expected an instance of Array.\\`); }`;\n\nconst codeGen = <T>(schema: Schema<T>, context: Context, valuePath: string, path: string): string => {\n if (schema instanceof And) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas.map((s) => `try { ${codeGen(s, context, valueAlias, path)} } catch (e) { ${errorsAlias}.push(e.message); }`).join('\\n');\n return `// And\n const ${errorsAlias} = [];\n const ${valueAlias} = ${valuePath};\n ${code}\n ${codeGenExpectNoErrors(errorsAlias)}\n`;\n } else if (schema instanceof Or) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code = schema.schemas\n .map((s) => codeGen(s, context, valueAlias, path))\n .reduceRight((result, code) => codeGenCollectErrors(errorsAlias, code, result), codeGenExpectNoErrors(errorsAlias));\n return `// Or\nconst ${errorsAlias} = [];\nconst ${valueAlias} = ${valuePath};\n${code}\n `;\n } else if (schema instanceof Optional) {\n const valueAlias = context.unique('v');\n return `// Optional\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== undefined && ${valueAlias} !== null) { ${codeGen(schema.schemas[0], context, valueAlias, path)} }\n`;\n } else if (schema instanceof Tuple) {\n const valueAlias = context.unique('v');\n const errorsAlias = context.unique('err');\n const code: string[] = [\n '// Tuple',\n `const ${valueAlias} = ${valuePath};`,\n `const ${errorsAlias} = [];`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n `if (${valueAlias}.length > ${schema.schemas.length}) { throw new TypeError(\\`Invalid tuple length \\${${valueAlias}.length} for path \"${path}\", expected ${schema.schemas.length}.\\`); }`,\n ...schema.schemas.map((s, idx) => codeGenCollectErrors(errorsAlias, codeGen(s, context, `${valueAlias}[${idx}]`, `${path}[${idx}]`))),\n codeGenExpectNoErrors(errorsAlias),\n ];\n return code.join('\\n');\n } else if (typeof schema === 'function') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n `const ${registryAlias} = ctx.registry[${index}];`,\n codeGenExpectNonNullable(valueAlias, path),\n ];\n if ((schema as unknown) !== Error && !(schema?.prototype instanceof Error)) {\n code.push(codeGenExpectNonError(valueAlias, path));\n }\n\n code.push(\n `if (typeof ${valueAlias} === 'object' && !(${valueAlias} instanceof ${registryAlias})) { throw new TypeError(\\`Invalid instance of \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected an instance of ${schema?.name}\\`); }`,\n `if (typeof ${valueAlias} !== 'object' && ${valueAlias}?.constructor !== ${registryAlias}) { throw new TypeError(\\`Invalid type \\${${valueAlias}?.constructor?.name} for path \"${path}\", expected type ${schema?.name}\\`); }`,\n `if (Number.isNaN(${valueAlias}?.valueOf?.())) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected a valid ${schema?.name}\\`); }`,\n );\n return code.join('\\n');\n } else if (Array.isArray(schema)) {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectNonError(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Array'),\n codeGenExpectArray(valueAlias, path),\n ];\n if (schema.length > 0) {\n const value = context.unique('val');\n const key = context.unique('key');\n const errorsAlias = context.unique('err');\n code.push(`const ${errorsAlias} = [];`);\n code.push(\n ...schema.map(\n (s) => `${valueAlias}.forEach((${value},${key}) => { ${codeGenCollectErrors(errorsAlias, codeGen(s, context, value, `${path}[\\${${key}}]`))} });`,\n ),\n );\n\n code.push(codeGenExpectNoErrors(errorsAlias));\n }\n return code.join('\\n');\n } else if (typeof schema === 'object' && schema !== null) {\n if (schema instanceof RegExp) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\n${codeGenExpectNonNullable(valueAlias, path)}\n${codeGenExpectNonError(valueAlias, path)}\nif (!${schema.toString()}.test('' + ${valueAlias})) { throw new TypeError(\\`Invalid value \\${${valueAlias}} for path \"${path}\", expected to match ${schema.toString()}\\`); }\n`;\n } else {\n const valueAlias = context.unique('v');\n const code: string[] = [\n `const ${valueAlias} = ${valuePath};`,\n codeGenExpectNonNullable(valueAlias, path),\n codeGenExpectObject(valueAlias, path, 'Object'),\n codeGenExpectNonError(valueAlias, path),\n ];\n if ($keys in schema) {\n const keysAlias = context.unique('k');\n const errorsAlias = context.unique('err');\n const kAlias = context.unique('k');\n code.push(`\nconst ${keysAlias} = Object.keys(${valueAlias});\nconst ${errorsAlias} = [];\n${keysAlias}.forEach(${kAlias} => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$keys], context, kAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($values in schema) {\n const vAlias = context.unique('val');\n const kAlias = context.unique('k');\n const entriesAlias = context.unique('en');\n const errorsAlias = context.unique('err');\n code.push(`\nconst ${entriesAlias} = Object.entries(${valueAlias});\nconst ${errorsAlias} = [];\n${entriesAlias}.forEach(([${kAlias},${vAlias}]) => { ${codeGenCollectErrors(errorsAlias, codeGen(schema[$values], context, vAlias, `${path}[\\${${kAlias}}]`))} });\n${codeGenExpectNoErrors(errorsAlias)}\n`);\n }\n if ($strict in schema && schema[$strict]) {\n const keysAlias = context.unique('k');\n const kAlias = context.unique('k');\n const extraAlias = context.unique('ex');\n code.push(`const ${keysAlias} = new Set(${JSON.stringify(Object.keys(schema))});`);\n code.push(`const ${extraAlias} = Object.keys(${valueAlias}).filter(${kAlias} => !${keysAlias}.has(${kAlias}));`);\n code.push(`if (${extraAlias}.length !== 0) { throw new TypeError(\\`Extra properties: \\${${extraAlias}}, are not allowed for path \"${path}\"\\`); }`);\n }\n code.push(...Object.entries(schema).map(([key, s]) => codeGen(s, context, `${valueAlias}['${key}']`, `${path}.${key}`)));\n return `${code.join('\\n')}`;\n }\n } else if (typeof schema === 'symbol') {\n const index = context.register(schema);\n const valueAlias = context.unique('v');\n const registryAlias = context.unique('r');\n\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${registryAlias} = ctx.registry[${index}];\nif (typeof ${valueAlias} !== 'symbol') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected symbol\\`); }\nif (${valueAlias} !== ${registryAlias}) { throw new TypeError(\\`Invalid value \\${${valueAlias}.toString()} for path \"${path}\", expected ${schema.toString()}\\`); }\n `;\n } else if (schema === null || schema === undefined) {\n const valueAlias = context.unique('v');\n return `\nconst ${valueAlias} = ${valuePath};\nif (${valueAlias} !== null && ${valueAlias} !== undefined ) { throw new TypeError(\\`Invalid value \\${JSON.stringify(${valueAlias})} for path \"${path}\", expected nullable\\`); }\n `;\n } else {\n const valueAlias = context.unique('v');\n const value = context.unique('val');\n return `\nconst ${valueAlias} = ${valuePath};\nconst ${value} = ${JSON.stringify(schema)};\n${codeGenExpectNonError(valueAlias, path)}\nif (typeof ${valueAlias} !== '${typeof schema}') { throw new TypeError(\\`Invalid type \\${typeof ${valueAlias}} for path \"${path}\", expected ${typeof schema}\\`); }\nif (${valueAlias} !== ${value}) { throw new TypeError(\\`Invalid value \\${JSON.stringify(${valueAlias})} for path \"${path}\", expected ${JSON.stringify(schema)}\\`); }\n`;\n }\n};\n\n/**\n * Compiles a schema into a validation function.\n *\n * This function takes a schema definition and generates a JavaScript function\n * that can be used to validate data against the schema.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to compile.\n * @param rootName - A name for the root of the data structure (used in error messages).\n * @returns A validation function that takes data as input and throws a TypeError if the data does not conform to the schema.\n *\n * @example\n * ```typescript\n * import { compile, optional, and, or } from 'ascertain';\n *\n * const userSchema = {\n * name: String,\n * age: Number,\n * email: optional(String),\n * role: or('admin', 'user', 'guest')\n * };\n *\n * const validateUser = compile(userSchema, 'User');\n *\n * // Valid data - no error thrown\n * validateUser({\n * name: 'John Doe',\n * age: 30,\n * email: 'john@example.com',\n * role: 'user'\n * });\n *\n * // Invalid data - throws TypeError\n * try {\n * validateUser({\n * name: 123, // Invalid: should be string\n * age: 'thirty' // Invalid: should be number\n * });\n * } catch (error) {\n * console.error(error.message); // Detailed validation errors\n * }\n * ```\n */\nexport const compile = <T>(schema: Schema<T>, rootName: string) => {\n const context = new Context();\n const code = codeGen(schema, context, 'data', rootName);\n const validator = new Function('ctx', 'data', code);\n return (data: T) => validator(context, data);\n};\n\n/**\n * Asserts that data conforms to a given schema.\n *\n * This function is a convenient wrapper around `compile`. It compiles the schema\n * and immediately validates the provided data against it.\n *\n * @template T - The type of data the schema validates.\n * @param schema - The schema to validate against.\n * @param data - The data to validate.\n * @param rootName - A name for the root of the data structure (used in error messages, defaults to '[root]').\n * @throws `{TypeError}` If the data does not conform to the schema.\n *\n * @example\n * ```typescript\n * import { ascertain, optional, and, or } from 'ascertain';\n *\n * const userSchema = {\n * name: String,\n * age: Number,\n * email: optional(String),\n * active: Boolean\n * };\n *\n * const userData = {\n * name: 'Alice',\n * age: 25,\n * email: 'alice@example.com',\n * active: true\n * };\n *\n * // Validate data - throws if invalid, otherwise continues silently\n * ascertain(userSchema, userData, 'UserData');\n * console.log('User data is valid!');\n *\n * // Example with invalid data\n * try {\n * ascertain(userSchema, {\n * name: 'Bob',\n * age: 'twenty-five', // Invalid: should be number\n * active: true\n * }, 'UserData');\n * } catch (error) {\n * console.error('Validation failed:', error.message);\n * }\n * ```\n *\n * @example\n * ```typescript\n * // Array validation\n * const numbersSchema = [Number];\n * const numbers = [1, 2, 3, 4, 5];\n *\n * ascertain(numbersSchema, numbers, 'Numbers');\n *\n * // Tuple validation\n * const coordinateSchema = tuple(Number, Number);\n * const point = [10, 20];\n *\n * ascertain(coordinateSchema, point, 'Point');\n * ```\n */\nexport const ascertain = <T>(schema: Schema<T>, data: T, rootName = '[root]') => {\n compile(schema, rootName)(data);\n};\n"],"names":["Operator","schemas","length","TypeError","name","$keys","Symbol","for","$values","$strict","Or","or","And","and","Optional","schema","optional","Tuple","tuple","fromBase64","Buffer","value","atob","from","toString","MULTIPLIERS","ms","s","m","h","d","w","asError","message","as","string","number","result","parseFloat","Number","isNaN","date","Date","parse","valueOf","time","conversionFactor","matches","match","amount","unit","parseInt","boolean","test","array","delimiter","split","json","JSON","base64","Context","registry","varIndex","register","includes","push","indexOf","unique","prefix","codeGenCollectErrors","errorsAlias","code","extra","codeGenExpectNoErrors","codeGenExpectNonError","valueAlias","path","codeGenExpectNonNullable","codeGenExpectObject","instanceOf","codeGenExpectArray","codeGen","context","valuePath","map","join","reduceRight","idx","index","registryAlias","Error","prototype","Array","isArray","key","RegExp","keysAlias","kAlias","vAlias","entriesAlias","extraAlias","stringify","Object","keys","entries","undefined","compile","rootName","validator","Function","data","ascertain"],"mappings":"AASA,MAAeA;;IACb,YAAY,AAAgBC,OAAoB,CAAE;aAAtBA,UAAAA;QAC1B,IAAIA,QAAQC,MAAM,KAAK,GAAG;YACxB,MAAM,IAAIC,UAAU,CAAC,iBAAiB,EAAE,IAAI,CAAC,WAAW,CAACC,IAAI,CAAC,+BAA+B,CAAC;QAChG;IACF;AACF;AAOA,OAAO,MAAMC,QAAQC,OAAOC,GAAG,CAAC,UAAU;AAI1C,OAAO,MAAMC,UAAUF,OAAOC,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1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ascertain",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "0-Deps, simple, fast, for browser and node js object schema validator",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -14,6 +14,17 @@
|
|
|
14
14
|
"build",
|
|
15
15
|
"src/index.js"
|
|
16
16
|
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "rm -rf build && NODE_ENV=production inop src/ build -i __tests__ && tsc --declaration --emitDeclarationOnly",
|
|
19
|
+
"lint": "eslint src",
|
|
20
|
+
"test": "NODE_ENV=test vitest run",
|
|
21
|
+
"docs:build": "vuepress build docs",
|
|
22
|
+
"docs:clean-dev": "vuepress dev docs --clean-cache",
|
|
23
|
+
"docs:dev": "vuepress dev docs",
|
|
24
|
+
"docs:gen": "node --loader ts-node/esm ./scripts/docgen.ts src/index.ts",
|
|
25
|
+
"docs:update-package": "pnpm dlx vp-update",
|
|
26
|
+
"prepare": "husky"
|
|
27
|
+
},
|
|
17
28
|
"repository": {
|
|
18
29
|
"type": "git",
|
|
19
30
|
"url": "git+https://github.com/3axap4eHko/ascertain.git"
|
|
@@ -49,6 +60,7 @@
|
|
|
49
60
|
"eslint": "^9.31.0",
|
|
50
61
|
"eslint-config-prettier": "^10.1.5",
|
|
51
62
|
"eslint-plugin-prettier": "^5.5.1",
|
|
63
|
+
"handlebars": "^4.7.8",
|
|
52
64
|
"husky": "^9.1.7",
|
|
53
65
|
"inop": "^0.7.8",
|
|
54
66
|
"overtake": "^0.1.2",
|
|
@@ -62,14 +74,5 @@
|
|
|
62
74
|
"vue": "^3.5.17",
|
|
63
75
|
"vuepress": "2.0.0-rc.24"
|
|
64
76
|
},
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
"lint": "eslint src",
|
|
68
|
-
"test": "NODE_ENV=test vitest run",
|
|
69
|
-
"docs:build": "vuepress build docs",
|
|
70
|
-
"docs:clean-dev": "vuepress dev docs --clean-cache",
|
|
71
|
-
"docs:dev": "vuepress dev docs",
|
|
72
|
-
"docs:gen": "node --loader ts-node/esm ./scripts/docgen.ts src/index.ts",
|
|
73
|
-
"docs:update-package": "pnpm dlx vp-update"
|
|
74
|
-
}
|
|
75
|
-
}
|
|
77
|
+
"packageManager": "pnpm@9.15.9"
|
|
78
|
+
}
|