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.62 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/string/regExp.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst regExpErrorMessage = 'The received value does not match the required text pattern';\nconst regExpErrorKey = 's:regExp';\n\n/**\n * @description Asserts that a string value matches a specified regular expression pattern.\n * @param {RegExp} expected The regular expression pattern that the string value should match.\n * @returns {RequiredValidation} A validation function that takes a received string and an exception context.\n * @throws {ValidationError} if the received value does not match the expected pattern.\n * @example\n * const schema = string().custom(regExp(/^[A-Za-z0-9]+$/)); // Validates against alphanumeric pattern\n * parseOrFail(schema, 'valid123'); // Valid\n * parseOrFail(schema, 'invalid!@#'); // Throws an error: 'The received value does not match the required text pattern'\n *\n * @translation Error Translation Key = 's:regExp'\n */\nexport const regExp =\n (expected: RegExp): RequiredValidation =>\n (received: string, ctx: ExceptionContext) => {\n if (!expected.test(received)) ctx.addIssue(expected, received, regExpErrorMessage);\n };\n\nregExp.key = regExpErrorKey;\nregExp.message = regExpErrorMessage;\nsetToDefaultLocale(regExp);\n"],"mappings":";;;;;AAGA,IAAM,qBAAqB;AAC3B,IAAM,iBAAiB;AAchB,IAAM,SACX,CAAC,aACD,CAAC,UAAkB,QAA0B;AAC3C,MAAI,CAAC,SAAS,KAAK,QAAQ,EAAG,KAAI,SAAS,UAAU,UAAU,kBAAkB;AACnF;AAEF,OAAO,MAAM;AACb,OAAO,UAAU;AACjB,mBAAmB,MAAM;","names":[]}