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.22 kB
{"version":3,"sources":["../src/asserts/number/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 number values.\n * @returns {NumberSchema} A new instance of `NumberSchema` for validating numbers.\n * @example\n * const schema = number();\n * parseOrFail(schema, 42); // Validates successfully\n * parseOrFail(schema, '42'); // Throws a validation error\n *\n * @instance Of NumberSchema\n */\nexport function number(): WithBGuardType<NumberSchema, number> {\n return new NumberSchema({ type: ['number'], requiredValidations: [] }) as WithBGuardType<NumberSchema, number>;\n}\n\nclass NumberSchema extends CommonSchema {\n protected _number = 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 * number().equalTo(42); // Infers the type 42\n *\n * @public\n */\n public equalTo<Y extends number>(expectedValue: Y): WithBGuardType<this, Y> {\n if (this.limit) throw new BuildSchemaError(ONLY_ONCE);\n this.limit = true;\n _setStrictType(this, expectedValue);\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 * number().oneOfValues([5, 7]); // Infers the type 5 | 7\n *\n * @public\n */\n public oneOfValues<Y extends number>(expectedValue: Y[]): WithBGuardType<this, Y> {\n if (this.limit) throw new BuildSchemaError(ONLY_ONCE);\n this.limit = true;\n _setStrictType(this, expectedValue);\n return this.custom(oneOfValues(expectedValue)) as WithBGuardType<this, Y>;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAkBO,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,QAAI,KAAK,MAAO,OAAM,IAAI,iBAAiB,SAAS;AACpD,SAAK,QAAQ;AACb,mBAAe,MAAM,aAAa;AAClC,WAAO,KAAK,OAAO,QAAQ,aAAa,CAAC;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYO,YAA8B,eAA6C;AAChF,QAAI,KAAK,MAAO,OAAM,IAAI,iBAAiB,SAAS;AACpD,SAAK,QAAQ;AACb,mBAAe,MAAM,aAAa;AAClC,WAAO,KAAK,OAAO,YAAY,aAAa,CAAC;AAAA,EAC/C;AACF;","names":[]}