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.53 kB
{"version":3,"sources":["../src/asserts/string/email.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst emailRegExp = /^[^@]+@[^@]+\\.[^@]+$/;\nconst emailErrorMessage = 'The received value does not match the required email pattern';\nconst emailErrorKey = 's:email';\n\n/**\n * @description Asserts that a string value matches the email pattern. The pattern checks for a basic email format.\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 email pattern.\n * @example\n * const schema = string().custom(email());\n * parseOrFail(schema, 'example@example.com'); // Valid\n * parseOrFail(schema, 'invalid-email'); // Throws an error: 'The received value does not match the required email pattern'\n *\n * @translation - Error Translation Key = 's:email'\n */\nexport const email = (): RequiredValidation => (received: string, ctx: ExceptionContext) => {\n if (!emailRegExp.test(received)) ctx.addIssue(emailRegExp, received, emailErrorMessage);\n};\n\nemail.key = emailErrorKey;\nemail.message = emailErrorMessage;\nsetToDefaultLocale(email);\n"],"mappings":";;;;;AAGA,IAAM,cAAc;AACpB,IAAM,oBAAoB;AAC1B,IAAM,gBAAgB;AAaf,IAAM,QAAQ,MAA0B,CAAC,UAAkB,QAA0B;AAC1F,MAAI,CAAC,YAAY,KAAK,QAAQ,EAAG,KAAI,SAAS,aAAa,UAAU,iBAAiB;AACxF;AAEA,MAAM,MAAM;AACZ,MAAM,UAAU;AAChB,mBAAmB,KAAK;","names":[]}