UNPKG

bguard

Version:

**bguard** is a powerful, flexible, and type-safe validation library for TypeScript. It allows developers to define validation schemas for their data structures and ensures that data conforms to the expected types and constraints.

1 lines 3.51 kB
{"version":3,"sources":["../src/asserts/bigint/index.ts"],"sourcesContent":["import { WithBGuardType } from '../../commonTypes';\nimport { BuildSchemaError } from '../../exceptions';\nimport { ONLY_ONCE } from '../../helpers/constants';\nimport { CommonSchema } from '../../core';\nimport { _setStrictType } from '../../helpers/setStrictType';\nimport { equalTo } from '../mix/equalTo';\nimport { oneOfValues } from '../mix/oneOfValues';\n\n/**\n * @description - Creates a new schema for validating bigint values.\n * @returns {WithBGuardType<BigIntSchema, bigint>} A new instance of `BigIntSchema` for validating bigints.\n * @example\n * const schema = bigint();\n * parseOrFail(schema, 42n); // Validates successfully\n * parseOrFail(schema, 42); // Throws a validation error\n * parseOrFail(schema, '42'); // Throws a validation error\n *\n * @instance Of BigIntSchema\n */\nexport function bigint(): WithBGuardType<BigIntSchema, bigint> {\n return new BigIntSchema({ type: ['bigint'], requiredValidations: [] }) as WithBGuardType<BigIntSchema, bigint>;\n}\n\nclass BigIntSchema extends CommonSchema {\n protected _bigint = 1;\n private limit: boolean | undefined;\n\n /**\n * @method equalTo\n * @description Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.\n * @param expectedValue - The value that the schema must exactly match.\n * @returns The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type\n * @example\n * bigint().equalTo(42n); // Infers the type 42n\n *\n * @public\n */\n public equalTo<Y extends bigint>(expectedValue: Y): WithBGuardType<this, Y> {\n this.defaultValueCheck();\n this.limitCheck();\n _setStrictType(this, `${expectedValue}n`);\n return this.custom(equalTo(expectedValue)) as WithBGuardType<this, Y>;\n }\n\n /**\n * @method oneOfValues\n * @description Restricts the schema to match one of the specified values and infers the union of those values as the TypeScript type.\n * @param expectedValues - An array of values that the schema can match.\n * @returns The schema instance restricted to one of the specified values, with the union of those values inferred as the TypeScript type.\n * @example\n * bigint().oneOfValues([5n, 7n]); // Infers the type 5n | 7n\n *\n * @public\n */\n public oneOfValues<Y extends bigint>(expectedValue: Y[]): WithBGuardType<this, Y> {\n this.defaultValueCheck();\n this.limitCheck();\n _setStrictType(\n this,\n expectedValue.map((el) => `${el}n`),\n );\n return this.custom(oneOfValues(expectedValue)) as WithBGuardType<this, Y>;\n }\n\n private limitCheck() {\n if (this.limit) throw new BuildSchemaError(ONLY_ONCE);\n this.limit = true;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAmBO,SAAS,SAA+C;AAC7D,SAAO,IAAI,aAAa,EAAE,MAAM,CAAC,QAAQ,GAAG,qBAAqB,CAAC,EAAE,CAAC;AACvE;AAEA,IAAM,eAAN,cAA2B,aAAa;AAAA,EAC5B,UAAU;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYD,QAA0B,eAA2C;AAC1E,SAAK,kBAAkB;AACvB,SAAK,WAAW;AAChB,mBAAe,MAAM,GAAG,aAAa,GAAG;AACxC,WAAO,KAAK,OAAO,QAAQ,aAAa,CAAC;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYO,YAA8B,eAA6C;AAChF,SAAK,kBAAkB;AACvB,SAAK,WAAW;AAChB;AAAA,MACE;AAAA,MACA,cAAc,IAAI,CAAC,OAAO,GAAG,EAAE,GAAG;AAAA,IACpC;AACA,WAAO,KAAK,OAAO,YAAY,aAAa,CAAC;AAAA,EAC/C;AAAA,EAEQ,aAAa;AACnB,QAAI,KAAK,MAAO,OAAM,IAAI,iBAAiB,SAAS;AACpD,SAAK,QAAQ;AAAA,EACf;AACF;","names":[]}