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.67 kB
{"version":3,"sources":["../src/asserts/string/uuidV2.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst uuidV2ErrorMessage = 'The received value is not a valid UUID v2';\nconst uuidV2ErrorKey = 's:uuidV2';\n\nconst uuidV2Pattern = /^[0-9a-f]{8}-[0-9a-f]{4}-2[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\n\n/**\n * @description Asserts that a string value matches the UUID v2 format.\n * @returns {RequiredValidation} A validation function that takes a received string and an exception context.\n * @throws {ValidationError} if the received value is not a valid UUID v2.\n * @example\n * const schema = string().custom(uuidV2());\n * parseOrFail(schema, '550e8400-e29b-21d4-a716-446655440000'); // Valid\n * parseOrFail(schema, '550e8400-e29b-31d4-d716-446655440000'); // Throws an error: 'The received value is not a valid UUID v2'\n * parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v2'\n *\n * @translation Error Translation Key = 's:uuidV2'\n */\nexport const uuidV2 = (): RequiredValidation => (received: string, ctx: ExceptionContext) => {\n if (!uuidV2Pattern.test(received)) {\n ctx.addIssue('uuid v2', received, uuidV2ErrorKey);\n }\n};\n\nuuidV2.key = uuidV2ErrorKey;\nuuidV2.message = uuidV2ErrorMessage;\nsetToDefaultLocale(uuidV2);\n"],"mappings":";;;;;AAGA,IAAM,qBAAqB;AAC3B,IAAM,iBAAiB;AAEvB,IAAM,gBAAgB;AAcf,IAAM,SAAS,MAA0B,CAAC,UAAkB,QAA0B;AAC3F,MAAI,CAAC,cAAc,KAAK,QAAQ,GAAG;AACjC,QAAI,SAAS,WAAW,UAAU,cAAc;AAAA,EAClD;AACF;AAEA,OAAO,MAAM;AACb,OAAO,UAAU;AACjB,mBAAmB,MAAM;","names":[]}