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 2.7 kB
{"version":3,"sources":["../src/asserts/boolean/index.ts"],"sourcesContent":["import { WithBGuardType } from '../../commonTypes';\nimport { CommonSchema, type ExceptionContext } from '../../core';\nimport { _setStrictType } from '../../helpers/setStrictType';\n\nconst isBoolean = (expected: boolean) => (received: boolean, ctx: ExceptionContext) => {\n if (received !== expected) ctx.addIssue(expected, received, 'c:isBoolean');\n};\n\n/**\n * @description Creates a new schema for validating boolean values.\n * @returns {WithBGuardType<BooleanSchema, boolean>} A new instance of `BooleanSchema` for validating booleans.\n * @example\n * const schema = boolean();\n * parseOrFail(schema, true); // Validates successfully\n * parseOrFail(schema, 'true'); // Throws a validation error\n *\n * @instance Of BooleanSchema\n */\nexport function boolean(): WithBGuardType<BooleanSchema, boolean> {\n return new BooleanSchema({ type: ['boolean'], requiredValidations: [] }) as WithBGuardType<BooleanSchema, boolean>;\n}\n\nclass BooleanSchema extends CommonSchema {\n protected _boolean = 1;\n\n /**\n * @method onlyTrue\n * @description Restricts the schema to exactly match the boolean value true and infers the true value as the TypeScript type.\n * @returns The schema instance restricted to the value true, with the true value inferred as the TypeScript type\n * @example\n * boolean().onlyTrue(); // Infers the type true\n *\n * @public\n */\n public onlyTrue(): WithBGuardType<this, true> {\n _setStrictType(this, true);\n return this.custom(isBoolean(true)) as WithBGuardType<this, true>;\n }\n\n /**\n * @method onlyFalse\n * @description Restricts the schema to exactly match the boolean value false and infers the false value as the TypeScript type.\n * @returns The schema instance restricted to the value false, with the false value inferred as the TypeScript type\n * @example\n * boolean().onlyFalse(); // Infers the type false\n *\n * @public\n */\n public onlyFalse(): WithBGuardType<this, false> {\n _setStrictType(this, false);\n return this.custom(isBoolean(false)) as WithBGuardType<this, false>;\n }\n}\n"],"mappings":";;;;;;;;AAIA,IAAM,YAAY,CAAC,aAAsB,CAAC,UAAmB,QAA0B;AACrF,MAAI,aAAa,SAAU,KAAI,SAAS,UAAU,UAAU,aAAa;AAC3E;AAYO,SAAS,UAAkD;AAChE,SAAO,IAAI,cAAc,EAAE,MAAM,CAAC,SAAS,GAAG,qBAAqB,CAAC,EAAE,CAAC;AACzE;AAEA,IAAM,gBAAN,cAA4B,aAAa;AAAA,EAC7B,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWd,WAAuC;AAC5C,mBAAe,MAAM,IAAI;AACzB,WAAO,KAAK,OAAO,UAAU,IAAI,CAAC;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWO,YAAyC;AAC9C,mBAAe,MAAM,KAAK;AAC1B,WAAO,KAAK,OAAO,UAAU,KAAK,CAAC;AAAA,EACrC;AACF;","names":[]}