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.93 kB
Source Map (JSON)
{"version":3,"sources":["../src/asserts/string/uuidV4.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst uuidV4ErrorMessage = 'The received value is not a valid UUID v4';\nconst uuidV4ErrorKey = 's:uuidV4';\n\nconst uuidV4Pattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[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 v4 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 v4.\n * @example\n * const schema = string().custom(uuidV4());\n * parseOrFail(schema, '123e4567-e89b-42d3-a456-426614174000'); // Valid\n * parseOrFail(schema, '123e4567-e89b-12d3-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'\n * parseOrFail(schema, '123e4567-e89b-a2d3-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'\n * parseOrFail(schema, '123e4567-e89b-42d3-c456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'\n * parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v4'\n *\n * @translation Error Translation Key = 's:uuidV4'\n */\nexport const uuidV4 = (): RequiredValidation => (received: string, ctx: ExceptionContext) => {\n if (!uuidV4Pattern.test(received)) {\n ctx.addIssue('uuid v4', received, uuidV4ErrorKey);\n }\n};\n\nuuidV4.key = uuidV4ErrorKey;\nuuidV4.message = uuidV4ErrorMessage;\nsetToDefaultLocale(uuidV4);\n"],"mappings":";;;;;AAGA,IAAM,qBAAqB;AAC3B,IAAM,iBAAiB;AAEvB,IAAM,gBAAgB;AAgBf,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":[]}