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
Source Map (JSON)
{"version":3,"sources":["../src/asserts/string/startsWith.ts"],"sourcesContent":["import type { ExceptionContext, RequiredValidation } from '../../core';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst startsWithErrorMessage = 'The received value does not start with the required substring';\nconst startsWithErrorKey = 's:startsWith';\n\n/**\n * @description Asserts that a string value starts with a specified substring.\n * @param {string} substring The substring that the string value must start with.\n * @returns {RequiredValidation} A validation function that takes a received string and an exception context.\n * @throws {ValidationError} if the received value does not start with the required substring.\n * @example\n * const schema = string().custom(startsWith('foo'));\n * parseOrFail(schema, 'foobar'); // Valid\n * parseOrFail(schema, 'barfoo'); // Throws an error: 'The received value does not start with the required substring'\n *\n * @translation Error Translation Key = 's:startsWith'\n */\nexport const startsWith =\n (substring: string): RequiredValidation =>\n (received: string, ctx: ExceptionContext) => {\n if (!received.startsWith(substring)) {\n ctx.addIssue(`starts with '${substring}'`, received, startsWithErrorKey);\n }\n };\n\nstartsWith.key = startsWithErrorKey;\nstartsWith.message = startsWithErrorMessage;\nsetToDefaultLocale(startsWith);\n"],"mappings":";;;;;AAGA,IAAM,yBAAyB;AAC/B,IAAM,qBAAqB;AAcpB,IAAM,aACX,CAAC,cACD,CAAC,UAAkB,QAA0B;AAC3C,MAAI,CAAC,SAAS,WAAW,SAAS,GAAG;AACnC,QAAI,SAAS,gBAAgB,SAAS,KAAK,UAAU,kBAAkB;AAAA,EACzE;AACF;AAEF,WAAW,MAAM;AACjB,WAAW,UAAU;AACrB,mBAAmB,UAAU;","names":[]}