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 1.45 kB
{"version":3,"sources":["../src/asserts/number/positive.ts"],"sourcesContent":["import { setToDefaultLocale } from '../../translationMap';\nimport type { ExceptionContext, RequiredValidation } from '../../core';\n\nconst positiveErrorMessage = 'The received value is not a positive number';\nconst positiveErrorKey = 'n:positive';\n\n/**\n * @description Asserts that a number value is positive (greater than zero).\n * @returns {RequiredValidation} A validation function that takes a received number and an exception context.\n * @throws {ValidationError} if the received value is not positive.\n * @example\n * const schema = number().custom(positive());\n * parseOrFail(schema, 10); // Valid\n * parseOrFail(schema, 0); // Throws an error: 'The received value is not a positive number'\n * parseOrFail(schema, -5); // Throws an error: 'The received value is not a positive number'\n *\n * @translation Error Translation Key = 'n:positive'\n */\nexport const positive = (): RequiredValidation => (received: number, ctx: ExceptionContext) => {\n if (received <= 0) ctx.addIssue('positive', received, positiveErrorMessage);\n};\n\npositive.key = positiveErrorKey;\npositive.message = positiveErrorMessage;\nsetToDefaultLocale(positive);\n"],"mappings":";;;;;AAGA,IAAM,uBAAuB;AAC7B,IAAM,mBAAmB;AAclB,IAAM,WAAW,MAA0B,CAAC,UAAkB,QAA0B;AAC7F,MAAI,YAAY,EAAG,KAAI,SAAS,YAAY,UAAU,oBAAoB;AAC5E;AAEA,SAAS,MAAM;AACf,SAAS,UAAU;AACnB,mBAAmB,QAAQ;","names":[]}