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.49 kB
{"version":3,"sources":["../src/asserts/string/uuid.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst uuidErrorMessage = 'The received value is not a valid UUID';\nconst uuidErrorKey = 's:uuid';\n\nconst uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;\n\n/**\n * @description Asserts that a string value matches the UUID 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.\n * @example\n * const schema = string().custom(uuid());\n * parseOrFail(schema, '123e4567-e89b-12d3-a456-426614174000'); // Valid\n * parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID'\n *\n * @translation Error Translation Key = 's:uuid'\n */\nexport const uuid = (): RequiredValidation => (received: string, ctx: ExceptionContext) => {\n if (!uuidPattern.test(received)) {\n ctx.addIssue('uuid', received, uuidErrorKey);\n }\n};\n\nuuid.key = uuidErrorKey;\nuuid.message = uuidErrorMessage;\nsetToDefaultLocale(uuid);\n"],"mappings":";;;;;AAGA,IAAM,mBAAmB;AACzB,IAAM,eAAe;AAErB,IAAM,cAAc;AAab,IAAM,OAAO,MAA0B,CAAC,UAAkB,QAA0B;AACzF,MAAI,CAAC,YAAY,KAAK,QAAQ,GAAG;AAC/B,QAAI,SAAS,QAAQ,UAAU,YAAY;AAAA,EAC7C;AACF;AAEA,KAAK,MAAM;AACX,KAAK,UAAU;AACf,mBAAmB,IAAI;","names":[]}