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 2.44 kB
{"version":3,"sources":["../src/asserts/string/atLeastOneSpecialChar.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst atLeastOneSpecialCharErrorMessage = 'The received value does not contain at least one special character';\nconst atLeastOneSpecialCharErrorKey = 's:atLeastOneSpecialChar';\n\n/**\n * @description Asserts that a string value contains at least one special character.\n * @param {string} [allowedSpecialChars=*] The string containing allowed special characters. Defaults to '*@$!#%&()^~{}'.\n * @returns {RequiredValidation} A validation function that takes a received string and an exception context.\n * @throws {ValidationError} if the received value does not contain at least one of the allowed special characters.\n * @example\n * const schema = string().custom(atLeastOneSpecialChar()); // Default special characters\n * parseOrFail(schema, 'abc!def'); // Valid\n * parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'\n *\n * const customSchema = string().custom(atLeastOneSpecialChar('@$')); // Custom special characters\n * parseOrFail(customSchema, 'abc@def'); // Valid\n * parseOrFail(customSchema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'\n *\n * @translation Error Translation Key = 's:atLeastOneSpecialChar'\n */\nexport const atLeastOneSpecialChar = (allowedSpecialChars?: string): RequiredValidation => {\n const specialCharRegExp = new RegExp(\n `[${(allowedSpecialChars ?? '*@$!#%&()^~{}').replace(/[-/\\\\^$*+?.()|[\\]{}]/g, '\\\\$&')}]`,\n );\n return (received: string, ctx: ExceptionContext) => {\n if (!specialCharRegExp.test(received)) ctx.addIssue('special character', received, atLeastOneSpecialCharErrorKey);\n };\n};\n\natLeastOneSpecialChar.key = atLeastOneSpecialCharErrorKey;\natLeastOneSpecialChar.message = atLeastOneSpecialCharErrorMessage;\nsetToDefaultLocale(atLeastOneSpecialChar);\n"],"mappings":";;;;;AAGA,IAAM,oCAAoC;AAC1C,IAAM,gCAAgC;AAkB/B,IAAM,wBAAwB,CAAC,wBAAqD;AACzF,QAAM,oBAAoB,IAAI;AAAA,IAC5B,KAAK,uBAAuB,iBAAiB,QAAQ,yBAAyB,MAAM,CAAC;AAAA,EACvF;AACA,SAAO,CAAC,UAAkB,QAA0B;AAClD,QAAI,CAAC,kBAAkB,KAAK,QAAQ,EAAG,KAAI,SAAS,qBAAqB,UAAU,6BAA6B;AAAA,EAClH;AACF;AAEA,sBAAsB,MAAM;AAC5B,sBAAsB,UAAU;AAChC,mBAAmB,qBAAqB;","names":[]}