bguard 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/README.md +359 -25
  2. package/lib/asserts/number/index.js +4 -3
  3. package/lib/asserts/number/index.js.map +1 -1
  4. package/lib/asserts/number/index.mjs +3 -2
  5. package/lib/asserts/string/atLeastOneDigit.d.mts +20 -0
  6. package/lib/asserts/string/atLeastOneDigit.d.ts +20 -0
  7. package/lib/asserts/string/atLeastOneDigit.js +21 -0
  8. package/lib/asserts/string/atLeastOneDigit.js.map +1 -0
  9. package/lib/asserts/string/atLeastOneDigit.mjs +21 -0
  10. package/lib/asserts/string/atLeastOneDigit.mjs.map +1 -0
  11. package/lib/asserts/string/atLeastOneLowerChar.d.mts +20 -0
  12. package/lib/asserts/string/atLeastOneLowerChar.d.ts +20 -0
  13. package/lib/asserts/string/atLeastOneLowerChar.js +22 -0
  14. package/lib/asserts/string/atLeastOneLowerChar.js.map +1 -0
  15. package/lib/asserts/string/atLeastOneLowerChar.mjs +22 -0
  16. package/lib/asserts/string/atLeastOneLowerChar.mjs.map +1 -0
  17. package/lib/asserts/string/atLeastOneSpecialChar.d.mts +25 -0
  18. package/lib/asserts/string/atLeastOneSpecialChar.d.ts +25 -0
  19. package/lib/asserts/string/atLeastOneSpecialChar.js +26 -0
  20. package/lib/asserts/string/atLeastOneSpecialChar.js.map +1 -0
  21. package/lib/asserts/string/atLeastOneSpecialChar.mjs +26 -0
  22. package/lib/asserts/string/atLeastOneSpecialChar.mjs.map +1 -0
  23. package/lib/asserts/string/atLeastOneUpperChar.d.mts +20 -0
  24. package/lib/asserts/string/atLeastOneUpperChar.d.ts +20 -0
  25. package/lib/asserts/string/atLeastOneUpperChar.js +22 -0
  26. package/lib/asserts/string/atLeastOneUpperChar.js.map +1 -0
  27. package/lib/asserts/string/atLeastOneUpperChar.mjs +22 -0
  28. package/lib/asserts/string/atLeastOneUpperChar.mjs.map +1 -0
  29. package/lib/asserts/string/contains.d.mts +21 -0
  30. package/lib/asserts/string/contains.d.ts +21 -0
  31. package/lib/asserts/string/contains.js +22 -0
  32. package/lib/asserts/string/contains.js.map +1 -0
  33. package/lib/asserts/string/contains.mjs +22 -0
  34. package/lib/asserts/string/contains.mjs.map +1 -0
  35. package/lib/asserts/string/endsWith.d.mts +21 -0
  36. package/lib/asserts/string/endsWith.d.ts +21 -0
  37. package/lib/asserts/string/endsWith.js +22 -0
  38. package/lib/asserts/string/endsWith.js.map +1 -0
  39. package/lib/asserts/string/endsWith.mjs +22 -0
  40. package/lib/asserts/string/endsWith.mjs.map +1 -0
  41. package/lib/asserts/string/index.js +4 -3
  42. package/lib/asserts/string/index.js.map +1 -1
  43. package/lib/asserts/string/index.mjs +3 -2
  44. package/lib/asserts/string/lowerCase.d.mts +20 -0
  45. package/lib/asserts/string/lowerCase.d.ts +20 -0
  46. package/lib/asserts/string/lowerCase.js +20 -0
  47. package/lib/asserts/string/lowerCase.js.map +1 -0
  48. package/lib/asserts/string/lowerCase.mjs +20 -0
  49. package/lib/asserts/string/lowerCase.mjs.map +1 -0
  50. package/lib/asserts/string/maxLength.d.mts +1 -1
  51. package/lib/asserts/string/maxLength.d.ts +1 -1
  52. package/lib/asserts/string/maxLength.js.map +1 -1
  53. package/lib/asserts/string/maxLength.mjs.map +1 -1
  54. package/lib/asserts/string/minLength.d.mts +1 -1
  55. package/lib/asserts/string/minLength.d.ts +1 -1
  56. package/lib/asserts/string/minLength.js.map +1 -1
  57. package/lib/asserts/string/minLength.mjs.map +1 -1
  58. package/lib/asserts/string/regExp.d.mts +1 -1
  59. package/lib/asserts/string/regExp.d.ts +1 -1
  60. package/lib/asserts/string/regExp.js.map +1 -1
  61. package/lib/asserts/string/regExp.mjs.map +1 -1
  62. package/lib/asserts/string/startsWith.d.mts +21 -0
  63. package/lib/asserts/string/startsWith.d.ts +21 -0
  64. package/lib/asserts/string/startsWith.js +22 -0
  65. package/lib/asserts/string/startsWith.js.map +1 -0
  66. package/lib/asserts/string/startsWith.mjs +22 -0
  67. package/lib/asserts/string/startsWith.mjs.map +1 -0
  68. package/lib/asserts/string/upperCase.d.mts +21 -0
  69. package/lib/asserts/string/upperCase.d.ts +21 -0
  70. package/lib/asserts/string/upperCase.js +22 -0
  71. package/lib/asserts/string/upperCase.js.map +1 -0
  72. package/lib/asserts/string/upperCase.mjs +22 -0
  73. package/lib/asserts/string/upperCase.mjs.map +1 -0
  74. package/lib/asserts/string/uuid.d.mts +20 -0
  75. package/lib/asserts/string/uuid.d.ts +20 -0
  76. package/lib/asserts/string/uuid.js +23 -0
  77. package/lib/asserts/string/uuid.js.map +1 -0
  78. package/lib/asserts/string/uuid.mjs +23 -0
  79. package/lib/asserts/string/uuid.mjs.map +1 -0
  80. package/lib/asserts/string/uuidV1.d.mts +21 -0
  81. package/lib/asserts/string/uuidV1.d.ts +21 -0
  82. package/lib/asserts/string/uuidV1.js +23 -0
  83. package/lib/asserts/string/uuidV1.js.map +1 -0
  84. package/lib/asserts/string/uuidV1.mjs +23 -0
  85. package/lib/asserts/string/uuidV1.mjs.map +1 -0
  86. package/lib/asserts/string/uuidV2.d.mts +21 -0
  87. package/lib/asserts/string/uuidV2.d.ts +21 -0
  88. package/lib/asserts/string/uuidV2.js +23 -0
  89. package/lib/asserts/string/uuidV2.js.map +1 -0
  90. package/lib/asserts/string/uuidV2.mjs +23 -0
  91. package/lib/asserts/string/uuidV2.mjs.map +1 -0
  92. package/lib/asserts/string/uuidV3.d.mts +21 -0
  93. package/lib/asserts/string/uuidV3.d.ts +21 -0
  94. package/lib/asserts/string/uuidV3.js +23 -0
  95. package/lib/asserts/string/uuidV3.js.map +1 -0
  96. package/lib/asserts/string/uuidV3.mjs +23 -0
  97. package/lib/asserts/string/uuidV3.mjs.map +1 -0
  98. package/lib/asserts/string/uuidV4.d.mts +23 -0
  99. package/lib/asserts/string/uuidV4.d.ts +23 -0
  100. package/lib/asserts/string/uuidV4.js +23 -0
  101. package/lib/asserts/string/uuidV4.js.map +1 -0
  102. package/lib/asserts/string/uuidV4.mjs +23 -0
  103. package/lib/asserts/string/uuidV4.mjs.map +1 -0
  104. package/lib/asserts/string/uuidV5.d.mts +21 -0
  105. package/lib/asserts/string/uuidV5.d.ts +21 -0
  106. package/lib/asserts/string/uuidV5.js +23 -0
  107. package/lib/asserts/string/uuidV5.js.map +1 -0
  108. package/lib/asserts/string/uuidV5.mjs +23 -0
  109. package/lib/asserts/string/uuidV5.mjs.map +1 -0
  110. package/lib/asserts/string/validUrl.d.mts +23 -0
  111. package/lib/asserts/string/validUrl.d.ts +23 -0
  112. package/lib/asserts/string/validUrl.js +32 -0
  113. package/lib/asserts/string/validUrl.js.map +1 -0
  114. package/lib/asserts/string/validUrl.mjs +32 -0
  115. package/lib/asserts/string/validUrl.mjs.map +1 -0
  116. package/lib/{chunk-NTC2XQ36.mjs → chunk-32RSTOZ4.mjs} +2 -2
  117. package/lib/chunk-42J6CHIO.js +7 -0
  118. package/lib/chunk-42J6CHIO.js.map +1 -0
  119. package/lib/chunk-ASZW6XRM.mjs +7 -0
  120. package/lib/chunk-ASZW6XRM.mjs.map +1 -0
  121. package/lib/{chunk-FTLRX2XG.mjs → chunk-JPRCULRQ.mjs} +12 -1
  122. package/lib/chunk-JPRCULRQ.mjs.map +1 -0
  123. package/lib/{chunk-RQH4LXBW.js → chunk-KQKJASVB.js} +12 -1
  124. package/lib/chunk-KQKJASVB.js.map +1 -0
  125. package/lib/{chunk-F5MSBL33.js → chunk-LEQCDD66.js} +12 -1
  126. package/lib/chunk-LEQCDD66.js.map +1 -0
  127. package/lib/{chunk-CLG3QRZC.mjs → chunk-P7IXNCJG.mjs} +12 -1
  128. package/lib/chunk-P7IXNCJG.mjs.map +1 -0
  129. package/lib/{chunk-B6TFELXR.mjs → chunk-PXBAS3GA.mjs} +2 -2
  130. package/lib/{chunk-4RNK3I74.js → chunk-RD6TBCPQ.js} +3 -3
  131. package/lib/{chunk-4RNK3I74.js.map → chunk-RD6TBCPQ.js.map} +1 -1
  132. package/lib/{chunk-RL4MSI7R.js → chunk-TKZ6FLLR.js} +3 -3
  133. package/lib/{chunk-RL4MSI7R.js.map → chunk-TKZ6FLLR.js.map} +1 -1
  134. package/lib/helpers/constants.d.mts +3 -0
  135. package/lib/helpers/constants.d.ts +3 -0
  136. package/lib/helpers/constants.js +7 -0
  137. package/lib/helpers/constants.js.map +1 -0
  138. package/lib/helpers/constants.mjs +7 -0
  139. package/lib/helpers/constants.mjs.map +1 -0
  140. package/lib/index.js +8 -7
  141. package/lib/index.js.map +1 -1
  142. package/lib/index.mjs +8 -7
  143. package/lib/schemas/NumberSchema.d.mts +1 -0
  144. package/lib/schemas/NumberSchema.d.ts +1 -0
  145. package/lib/schemas/NumberSchema.js +3 -2
  146. package/lib/schemas/NumberSchema.js.map +1 -1
  147. package/lib/schemas/NumberSchema.mjs +2 -1
  148. package/lib/schemas/StringSchema.d.mts +1 -0
  149. package/lib/schemas/StringSchema.d.ts +1 -0
  150. package/lib/schemas/StringSchema.js +3 -2
  151. package/lib/schemas/StringSchema.js.map +1 -1
  152. package/lib/schemas/StringSchema.mjs +2 -1
  153. package/package.json +1 -1
  154. package/lib/chunk-CLG3QRZC.mjs.map +0 -1
  155. package/lib/chunk-F5MSBL33.js.map +0 -1
  156. package/lib/chunk-FTLRX2XG.mjs.map +0 -1
  157. package/lib/chunk-RQH4LXBW.js.map +0 -1
  158. /package/lib/{chunk-NTC2XQ36.mjs.map → chunk-32RSTOZ4.mjs.map} +0 -0
  159. /package/lib/{chunk-B6TFELXR.mjs.map → chunk-PXBAS3GA.mjs.map} +0 -0
package/README.md CHANGED
@@ -78,10 +78,8 @@ type StudentSchema = {
78
78
 
79
79
  ```
80
80
 
81
-
82
81
  #### Generating TypeScript Types with `codeGen`
83
82
 
84
-
85
83
  If you prefer to generate TypeScript types as a string, you can use the `codeGen` function:
86
84
 
87
85
  ```typeScript
@@ -112,6 +110,7 @@ This would output a string:
112
110
  verified?: boolean | undefined;
113
111
  }
114
112
  ```
113
+
115
114
  Note: The returned string does not include a type name or the `=` symbol. You would need to add these manually if you want a complete type definition.
116
115
 
117
116
  #### Generating Named TypeScript Types with `codeGenWithName`
@@ -133,7 +132,6 @@ console.log(namedTypeString);
133
132
 
134
133
  This would output a string:
135
134
 
136
-
137
135
  ```typeScript
138
136
  type StudentSchema = {
139
137
  email?: string | undefined;
@@ -147,13 +145,13 @@ type StudentSchema = {
147
145
  verified?: boolean | undefined;
148
146
  }
149
147
  ```
148
+
150
149
  #### Summary:
151
150
 
152
151
  `codeGen(schema: CommonSchema): string` - Generates a string of the TypeScript type based on the schema. You need to manually add a type name and assignment if needed.
153
152
 
154
153
  `codeGenWithName(typeName: string, schema: CommonSchema): string` - Generates a complete TypeScript type definition string, including the type keyword and type name.
155
154
 
156
-
157
155
  ### Validating Data
158
156
 
159
157
  This library provides two methods to parse data against schemas: `parse` and `parseOrFail`. These methods help in validating the data and obtaining structured errors if any issues are found during validation.
@@ -188,15 +186,12 @@ const invalidStudentData = {
188
186
  ],
189
187
  email: 'invalid-example',
190
188
  };
191
-
192
189
  ```
193
190
 
194
-
195
191
  #### `parse` Method
196
192
 
197
193
  The `parse` method validates the data and returns a tuple containing errors and the parsed value. This method allows you to choose whether to collect all errors or stop at the first error using an options flag.
198
194
 
199
-
200
195
  **Syntax:**
201
196
 
202
197
  ```typescript
@@ -235,16 +230,18 @@ try {
235
230
  console.error(error.message); // Logs the first validation error message, if any
236
231
  }
237
232
  ```
233
+
238
234
  Throws:
235
+
239
236
  - `ValidationError`: If any validation rule fails, this error is thrown with details of the first encountered error.
240
237
 
241
238
  Options:
242
- - `lng`: Specifies the language for error messages. Default is `'default'`.
243
239
 
240
+ - `lng`: Specifies the language for error messages. Default is `'default'`.
244
241
 
245
242
  ####
246
243
 
247
- ### Explanation
244
+ Explanation
248
245
 
249
246
  - **`parse` Method**: This method returns a tuple where the first element is an array of validation errors (if any), and the second element is the successfully parsed value (or `undefined` if errors exist). It allows collecting all errors by setting the `getAllErrors` flag.
250
247
 
@@ -276,9 +273,12 @@ const schema = string().nullable().optional();
276
273
  `boolean().onlyTrue()` will infer <b>true</b> as the type.
277
274
  `boolean().onlyFalse()` will infer <b>false</b> as the type.
278
275
 
279
- ### Custom Assertions
276
+ ### Custom (Library Built-in) Assertions
277
+
278
+ The `custom` method allows you to extend the validation schema with additional asserts. These asserts can either be user-defined or selected from the comprehensive set provided by the library. This flexibility ensures that you can tailor validations to meet specific requirements beyond the standard methods available.
279
+ All built-in asserts are documented in the [Built-in Custom Assert Documentation](#builtin_custom_assert_documentation) section.
280
280
 
281
- You can extend the validation with custom assertions:
281
+ Example
282
282
 
283
283
  ```typeScript
284
284
  import { min } from 'bguard/number/min';
@@ -287,10 +287,53 @@ import { max } from 'bguard/number/max';
287
287
  const ageSchema = number().custom(min(18), max(120));
288
288
  ```
289
289
 
290
- Assertions are imported from specific paths for better tree-shaking and smaller bundle sizes.
290
+ Library built-in assertions are imported from specific paths for better tree-shaking and smaller bundle sizes.
291
+
292
+ ### Create Custom Assertions
293
+
294
+ Bguard allows developers to create custom validation functions that can be integrated seamlessly with the library's existing functionality. Below is a detailed example demonstrating how to create a custom validation function, `minLength`, and how to properly document and map error messages for translations.
295
+
296
+ Example: Creating a `minLength` Custom Validation
297
+
298
+ ```typescript
299
+ import { guardException } from 'bguard/exceptions';
300
+ import { ExceptionContext, RequiredValidation } from 'bguard/commonTypes';
301
+ import { setToDefaultLocale } from 'bguard/translationMap';
302
+
303
+ const minLengthErrorMessage = 'The received value {{r}} is shorter than the expected length {{e}}';
304
+ const minLengthErrorKey = 'customPrefix:minLength';
305
+
306
+ export const minLength =
307
+ (expected: number): RequiredValidation =>
308
+ (received: string, ctx: ExceptionContext) => {
309
+ if (received.length < expected) {
310
+ guardException(expected, received, ctx, minLengthErrorKey);
311
+ }
312
+ };
313
+
314
+ minLength.key = minLengthErrorKey;
315
+ minLength.message = minLengthErrorMessage;
316
+ setToDefaultLocale(minLength);
317
+ ```
318
+
319
+ Explanation
320
+
321
+ - Error Key (`minLength.key`): This key (`'customPrefix:minLength'`) uniquely identifies the validation and is used for mapping error messages, especially when supporting multiple languages. It's essential to avoid collisions with built-in assertions, which use prefixes like `s:`, `n:`, and `b:` etc. More on that in [Common and Custom Translations](#common_and_custom_translations).
322
+
323
+ - Error Message (`minLength.message`): The message supports [interpolation](#translation), where `{{e}}` will be replaced by the expected value, and `{{r}}` will be replaced by the received value during validation .
324
+
325
+ - Exception Handling (`guardException`): This function is responsible for throwing the error when the validation fails. The `ctx` parameter must be passed to ensure the internal logic of the application works correctly.
326
+
327
+ - Localization Support (`setToDefaultLocale`): This function registers the default error message with its associated key. If you later decide to support multiple languages, you can easily map this key to different messages.
291
328
 
329
+ - Key Points for Developers:
292
330
 
293
- ### Translation
331
+ 1. Always create unique error keys for custom validations to avoid potential conflicts with Bguard's built-in validations.
332
+ 2. Custom validations should use prefixes other than `s:`, `n:`, `b:`, and similar ones reserved for Bguard's internal validations.
333
+ 3. The `minLengthErrorMessage` serves as the default message. If you want to provide translations, you can do so by mapping the error key in the translationMap.
334
+ For single-language applications, you can override the default message by directly passing your custom message to `guardException`.
335
+
336
+ ### Translation {#translation}
294
337
 
295
338
  Bguard provides default translations for error messages, but you can customize them as needed. Each potential error has an `errorKey` and `errorMessage`.
296
339
 
@@ -319,19 +362,17 @@ setLocale('SR', {
319
362
  // ... continue adding other translations
320
363
  });
321
364
  ```
365
+
322
366
  With this setup, in the translation namespace 'SR', if the received value is 4, you'll get an error message like:
323
367
 
324
368
  `'The received value 4 found on path .foo is less than expected value 5'`
325
369
 
326
- - `{{r}}` - Replaced with the received value.
327
- - `{{p}}` - Replaced with the path to the error.
328
- - `{{e}}` - Replaced with the expected value.
329
-
370
+ - `{{r}}` - Replaced with the received value.
371
+ - `{{p}}` - Replaced with the path to the error.
372
+ - `{{e}}` - Replaced with the expected value.
330
373
 
331
374
  > **Notice:** Do not overwrite the 'default' namespace. If a translation is missing, it will fall back to the 'default' translation.
332
375
 
333
-
334
-
335
376
  #### Using Translations
336
377
 
337
378
  To apply the new translation, both `parse` and `parseOrFail` functions accept a lng property in the options object provided as the third parameter:
@@ -342,8 +383,7 @@ parseOrFail(testSchema, { foo: 4 }, { lng: 'SR' });
342
383
  parse(testSchema, { foo: 4 }, { lng: 'SR' });
343
384
  ```
344
385
 
345
-
346
- #### Common and Custom Translations
386
+ #### Common and Custom Translations {#common_and_custom_translations}
347
387
 
348
388
  We have two sets of translations: common errors and specific assertions.
349
389
 
@@ -365,7 +405,7 @@ We have two sets of translations: common errors and specific assertions.
365
405
 
366
406
  For custom assertions, each key and message are located in separate files for better code splitting. There are multiple ways to identify a key:
367
407
 
368
- 1. Key Construction:
408
+ <b>1.</b> Key Construction:
369
409
  Keys are constructed as `'{typeId}:{functionName}'`, where `typeId` represents:
370
410
 
371
411
  - c - common
@@ -386,9 +426,10 @@ Example:
386
426
  ```typeScript
387
427
  import { maxLength } from 'bguard/string/maxLength';
388
428
  ```
429
+
389
430
  The function located in `'bguard/string/maxLength'` will have the key `'s:maxLength'`.
390
431
 
391
- 2. Assertion Function Properties:
432
+ <b>2.</b> Assertion Function Properties:
392
433
 
393
434
  Each assert function has two additional properties: `key` and `message`.
394
435
 
@@ -398,10 +439,12 @@ import { maxLength } from 'bguard/string/maxLength';
398
439
  console.log(maxLength.key); // Output: 's:maxLength'
399
440
  console.log(maxLength.message); // Output: 'The received value length is greater than expected'
400
441
  ```
442
+
401
443
  > **Notice:** Do not directly change these values.
402
444
 
403
- 3. IDE Support:
404
- Each key and message will be visible in text editors that support JSDoc IntelliSense.### Custom Assert Documentation
445
+ <b>3.</b> IDE Support:
446
+ Each key and message will be visible in text editors that support JSDoc IntelliSense.
447
+ ### Built-in Custom Assert Documentation {#builtin_custom_assert_documentation}
405
448
 
406
449
  #### mix
407
450
 
@@ -558,6 +601,97 @@ import { positive } from 'bguard/number/positive';
558
601
 
559
602
  #### string
560
603
 
604
+ ##### atLeastOneDigit (string)
605
+
606
+ ```typescript
607
+ import { atLeastOneDigit } from 'bguard/string/atLeastOneDigit';
608
+ ```
609
+
610
+ * _Description_ Asserts that a string value contains at least one digit.
611
+ * _Throws_ {ValidationError} if the received value does not contain at least one digit.
612
+ * _Example_
613
+ ```typescript
614
+ const schema = string().custom(atLeastOneDigit());
615
+ parseOrFail(schema, 'abc123'); // Valid
616
+ parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'
617
+ ```
618
+ * _See_ Error Translation Key = 's:atLeastOneDigit'
619
+
620
+
621
+ ##### atLeastOneLowerChar (string)
622
+
623
+ ```typescript
624
+ import { atLeastOneLowerChar } from 'bguard/string/atLeastOneLowerChar';
625
+ ```
626
+
627
+ * _Description_ Asserts that a string value contains at least one lowercase character.
628
+ * _Throws_ {ValidationError} if the received value does not contain at least one lowercase character.
629
+ * _Example_
630
+ ```typescript
631
+ const schema = string().custom(atLeastOneLowerChar());
632
+ parseOrFail(schema, 'abcDEF'); // Valid
633
+ parseOrFail(schema, 'ABCDEF'); // Throws an error: 'The received value does not contain at least one lowercase character'
634
+ ```
635
+ * _See_ Error Translation Key = 's:atLeastOneLowerChar'
636
+
637
+
638
+ ##### atLeastOneSpecialChar (string)
639
+
640
+ ```typescript
641
+ import { atLeastOneSpecialChar } from 'bguard/string/atLeastOneSpecialChar';
642
+ ```
643
+
644
+ * _Description_ Asserts that a string value contains at least one special character.
645
+ * _Param_ {string} [allowedSpecialChars=* '@!#%&()^~{}'] The string containing allowed special characters. Defaults to '*@!#%&()^~{}'.
646
+ * _Throws_ {ValidationError} if the received value does not contain at least one of the allowed special characters.
647
+ * _Example_
648
+ ```typescript
649
+ const schema = string().custom(atLeastOneSpecialChar()); // Default special characters
650
+ parseOrFail(schema, 'abc!def'); // Valid
651
+ parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
652
+
653
+ const customSchema = string().custom(atLeastOneSpecialChar('@$')); // Custom special characters
654
+ parseOrFail(customSchema, 'abc@def'); // Valid
655
+ parseOrFail(customSchema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
656
+ ```
657
+ * _See_ Error Translation Key = 's:atLeastOneSpecialChar'
658
+
659
+
660
+ ##### atLeastOneUpperChar (string)
661
+
662
+ ```typescript
663
+ import { atLeastOneUpperChar } from 'bguard/string/atLeastOneUpperChar';
664
+ ```
665
+
666
+ * _Description_ Asserts that a string value contains at least one uppercase character.
667
+ * _Throws_ {ValidationError} if the received value does not contain at least one uppercase character.
668
+ * _Example_
669
+ ```typescript
670
+ const schema = string().custom(atLeastOneUpperChar());
671
+ parseOrFail(schema, 'abcDEF'); // Valid
672
+ parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one uppercase character'
673
+ ```
674
+ * _See_ Error Translation Key = 's:atLeastOneUpperChar'
675
+
676
+
677
+ ##### contains (string)
678
+
679
+ ```typescript
680
+ import { contains } from 'bguard/string/contains';
681
+ ```
682
+
683
+ * _Description_ Asserts that a string value contains a specified substring.
684
+ * _Param_ {string} substring The substring that must be present in the string value.
685
+ * _Throws_ {ValidationError} if the received value does not contain the required substring.
686
+ * _Example_
687
+ ```typescript
688
+ const schema = string().custom(contains('foo'));
689
+ parseOrFail(schema, 'foobar'); // Valid
690
+ parseOrFail(schema, 'bar'); // Throws an error: 'The received value does not contain the required substring'
691
+ ```
692
+ * _See_ Error Translation Key = 's:contains'
693
+
694
+
561
695
  ##### email (string)
562
696
 
563
697
  ```typescript
@@ -575,6 +709,41 @@ import { email } from 'bguard/string/email';
575
709
  * _See_ - Error Translation Key = 's:email'
576
710
 
577
711
 
712
+ ##### endsWith (string)
713
+
714
+ ```typescript
715
+ import { endsWith } from 'bguard/string/endsWith';
716
+ ```
717
+
718
+ * _Description_ Asserts that a string value ends with a specified substring.
719
+ * _Param_ {string} substring The substring that the string value must end with.
720
+ * _Throws_ {ValidationError} if the received value does not end with the required substring.
721
+ * _Example_
722
+ ```typescript
723
+ const schema = string().custom(endsWith('bar'));
724
+ parseOrFail(schema, 'foobar'); // Valid
725
+ parseOrFail(schema, 'foofoo'); // Throws an error: 'The received value does not end with the required substring'
726
+ ```
727
+ * _See_ Error Translation Key = 's:endsWith'
728
+
729
+
730
+ ##### lowerCase (string)
731
+
732
+ ```typescript
733
+ import { lowerCase } from 'bguard/string/lowerCase';
734
+ ```
735
+
736
+ * _Description_ Asserts that a string value is in lowercase.
737
+ * _Throws_ {ValidationError} if the received value is not in lowercase.
738
+ * _Example_
739
+ ```typescript
740
+ const schema = string().custom(lowerCase());
741
+ parseOrFail(schema, 'valid'); // Valid
742
+ parseOrFail(schema, 'Invalid'); // Throws an error: 'The received value is not in lowercase'
743
+ ```
744
+ * _See_ Error Translation Key = 's:lowerCase'
745
+
746
+
578
747
  ##### maxLength (string)
579
748
 
580
749
  ```typescript
@@ -628,5 +797,170 @@ import { regExp } from 'bguard/string/regExp';
628
797
  ```
629
798
  * _See_ Error Translation Key = 's:regExp'
630
799
 
800
+
801
+ ##### startsWith (string)
802
+
803
+ ```typescript
804
+ import { startsWith } from 'bguard/string/startsWith';
805
+ ```
806
+
807
+ * _Description_ Asserts that a string value starts with a specified substring.
808
+ * _Param_ {string} substring The substring that the string value must start with.
809
+ * _Throws_ {ValidationError} if the received value does not start with the required substring.
810
+ * _Example_
811
+ ```typescript
812
+ const schema = string().custom(startsWith('foo'));
813
+ parseOrFail(schema, 'foobar'); // Valid
814
+ parseOrFail(schema, 'barfoo'); // Throws an error: 'The received value does not start with the required substring'
815
+ ```
816
+ * _See_ Error Translation Key = 's:startsWith'
817
+
818
+
819
+ ##### upperCase (string)
820
+
821
+ ```typescript
822
+ import { upperCase } from 'bguard/string/upperCase';
823
+ ```
824
+
825
+ * _Description_ Asserts that a string value is entirely in uppercase.
826
+ * _Throws_ {ValidationError} if the received value is not in uppercase.
827
+ * _Example_
828
+ ```typescript
829
+ const schema = string().custom(upperCase());
830
+ parseOrFail(schema, 'VALID'); // Valid
831
+ parseOrFail(schema, 'INVALID'); // Throws an error: 'The received value is not in uppercase'
832
+ parseOrFail(schema, 'Valid'); // Throws an error: 'The received value is not in uppercase'
833
+ ```
834
+ * _See_ Error Translation Key = 's:upperCase'
835
+
836
+
837
+ ##### uuid (string)
838
+
839
+ ```typescript
840
+ import { uuid } from 'bguard/string/uuid';
841
+ ```
842
+
843
+ * _Description_ Asserts that a string value matches the UUID format.
844
+ * _Throws_ {ValidationError} if the received value is not a valid UUID.
845
+ * _Example_
846
+ ```typescript
847
+ const schema = string().custom(uuid());
848
+ parseOrFail(schema, '123e4567-e89b-12d3-a456-426614174000'); // Valid
849
+ parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID'
850
+ ```
851
+ * _See_ Error Translation Key = 's:uuid'
852
+
853
+
854
+ ##### uuidV1 (string)
855
+
856
+ ```typescript
857
+ import { uuidV1 } from 'bguard/string/uuidV1';
858
+ ```
859
+
860
+ * _Description_ Asserts that a string value matches the UUID v1 format.
861
+ * _Throws_ {ValidationError} if the received value is not a valid UUID v1.
862
+ * _Example_
863
+ ```typescript
864
+ const schema = string().custom(uuidV1());
865
+ parseOrFail(schema, '550e8400-e29b-11d4-a716-446655440000'); // Valid
866
+ parseOrFail(schema, '550e8400-e29b-21d4-a716-446655440000'); // Throws an error: 'The received value is not a valid UUID v1'
867
+ parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v1'
868
+ ```
869
+ * _See_ Error Translation Key = 's:uuidV1'
870
+
871
+
872
+ ##### uuidV2 (string)
873
+
874
+ ```typescript
875
+ import { uuidV2 } from 'bguard/string/uuidV2';
876
+ ```
877
+
878
+ * _Description_ Asserts that a string value matches the UUID v2 format.
879
+ * _Throws_ {ValidationError} if the received value is not a valid UUID v2.
880
+ * _Example_
881
+ ```typescript
882
+ const schema = string().custom(uuidV2());
883
+ parseOrFail(schema, '550e8400-e29b-21d4-a716-446655440000'); // Valid
884
+ parseOrFail(schema, '550e8400-e29b-31d4-d716-446655440000'); // Throws an error: 'The received value is not a valid UUID v2'
885
+ parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v2'
886
+ ```
887
+ * _See_ Error Translation Key = 's:uuidV2'
888
+
889
+
890
+ ##### uuidV3 (string)
891
+
892
+ ```typescript
893
+ import { uuidV3 } from 'bguard/string/uuidV3';
894
+ ```
895
+
896
+ * _Description_ Asserts that a string value matches the UUID v3 format.
897
+ * _Throws_ {ValidationError} if the received value is not a valid UUID v3.
898
+ * _Example_
899
+ ```typescript
900
+ const schema = string().custom(uuidV3());
901
+ parseOrFail(schema, '550e8400-e29b-38d1-a456-426614174000'); // Valid
902
+ parseOrFail(schema, '550e8400-e29b-28d1-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v3'
903
+ parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v3'
904
+ ```
905
+ * _See_ Error Translation Key = 's:uuidV3'
906
+
907
+
908
+ ##### uuidV4 (string)
909
+
910
+ ```typescript
911
+ import { uuidV4 } from 'bguard/string/uuidV4';
912
+ ```
913
+
914
+ * _Description_ Asserts that a string value matches the UUID v4 format.
915
+ * _Throws_ {ValidationError} if the received value is not a valid UUID v4.
916
+ * _Example_
917
+ ```typescript
918
+ const schema = string().custom(uuidV4());
919
+ parseOrFail(schema, '123e4567-e89b-42d3-a456-426614174000'); // Valid
920
+ parseOrFail(schema, '123e4567-e89b-12d3-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'
921
+ parseOrFail(schema, '123e4567-e89b-a2d3-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'
922
+ parseOrFail(schema, '123e4567-e89b-42d3-c456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'
923
+ parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v4'
924
+ ```
925
+ * _See_ Error Translation Key = 's:uuidV4'
926
+
927
+
928
+ ##### uuidV5 (string)
929
+
930
+ ```typescript
931
+ import { uuidV5 } from 'bguard/string/uuidV5';
932
+ ```
933
+
934
+ * _Description_ Asserts that a string value matches the UUID v5 format.
935
+ * _Throws_ {ValidationError} if the received value is not a valid UUID v5.
936
+ * _Example_
937
+ ```typescript
938
+ const schema = string().custom(uuidV5());
939
+ parseOrFail(schema, '550e8400-e29b-51d4-a716-446655440000'); // Valid
940
+ parseOrFail(schema, '550e8400-e29b-41d4-a716-446655440000'); // Throws an error: 'The received value is not a valid UUID v5'
941
+ parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v5'
942
+ ```
943
+ * _See_ Error Translation Key = 's:uuidV5'
944
+
945
+
946
+ ##### validUrl (string)
947
+
948
+ ```typescript
949
+ import { validUrl } from 'bguard/string/validUrl';
950
+ ```
951
+
952
+ * _Description_ Asserts that the string value is a valid URL with optional protocol validation.
953
+ * _Param_ {string} [protocol] The protocol that the URL must start with (e.g., 'http'). If not provided, any URL starting with 'http://' or 'https://' is considered valid.
954
+ * _Throws_ {ValidationError} if the received value does not match the expected URL pattern.
955
+ * _Example_
956
+ ```typescript
957
+ const schema = string().custom(validUrl()); // Validates any URL starting with 'http://' or 'https://'
958
+ parseOrFail(schema, 'http://example.com'); // Valid
959
+ parseOrFail(schema, 'https://example.com'); // Valid
960
+ parseOrFail(schema, 'ftp://example.com'); // Throws an error
961
+ parseOrFail(schema, 'http:example.com'); // Throws an error
962
+ ```
963
+ * _See_ Error Translation Key = 's:url'
964
+
631
965
  ### Contributing
632
966
  Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.
@@ -1,9 +1,10 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunk4RNK3I74js = require('../../chunk-4RNK3I74.js');
4
- require('../../chunk-RQH4LXBW.js');
3
+ var _chunkRD6TBCPQjs = require('../../chunk-RD6TBCPQ.js');
4
+ require('../../chunk-KQKJASVB.js');
5
5
  require('../../chunk-J4VKFJQK.js');
6
6
  require('../../chunk-4CYIMDP2.js');
7
+ require('../../chunk-42J6CHIO.js');
7
8
  require('../../chunk-JSGZ7DKT.js');
8
9
  require('../../chunk-EGEHSELT.js');
9
10
  require('../../chunk-IRG4OQAB.js');
@@ -11,5 +12,5 @@ require('../../chunk-5BVJ6YMH.js');
11
12
  require('../../chunk-WK4O3HH4.js');
12
13
 
13
14
 
14
- exports.number = _chunk4RNK3I74js.number;
15
+ exports.number = _chunkRD6TBCPQjs.number;
15
16
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/bguard/bguard/lib/asserts/number/index.js"],"names":[],"mappings":"AAAA;AACE;AACF,0DAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC;AACE;AACF,yCAAC","file":"/home/runner/work/bguard/bguard/lib/asserts/number/index.js"}
1
+ {"version":3,"sources":["/home/runner/work/bguard/bguard/lib/asserts/number/index.js"],"names":[],"mappings":"AAAA;AACE;AACF,0DAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC,mCAAgC;AAChC;AACE;AACF,yCAAC","file":"/home/runner/work/bguard/bguard/lib/asserts/number/index.js"}
@@ -1,9 +1,10 @@
1
1
  import {
2
2
  number
3
- } from "../../chunk-B6TFELXR.mjs";
4
- import "../../chunk-CLG3QRZC.mjs";
3
+ } from "../../chunk-PXBAS3GA.mjs";
4
+ import "../../chunk-P7IXNCJG.mjs";
5
5
  import "../../chunk-NNFYXHLU.mjs";
6
6
  import "../../chunk-3O4WCRC2.mjs";
7
+ import "../../chunk-ASZW6XRM.mjs";
7
8
  import "../../chunk-RYVUYVEE.mjs";
8
9
  import "../../chunk-DPBE7TAQ.mjs";
9
10
  import "../../chunk-PH2PMUZP.mjs";
@@ -0,0 +1,20 @@
1
+ import { RequiredValidation } from '../../commonTypes.mjs';
2
+
3
+ /**
4
+ * @description Asserts that a string value contains at least one digit.
5
+ * @returns {RequiredValidation} A validation function that takes a received string and an exception context.
6
+ * @throws {ValidationError} if the received value does not contain at least one digit.
7
+ * @example
8
+ * const schema = string().custom(atLeastOneDigit());
9
+ * parseOrFail(schema, 'abc123'); // Valid
10
+ * parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'
11
+ *
12
+ * @translation Error Translation Key = 's:atLeastOneDigit'
13
+ */
14
+ declare const atLeastOneDigit: {
15
+ (): RequiredValidation;
16
+ key: string;
17
+ message: string;
18
+ };
19
+
20
+ export { atLeastOneDigit };
@@ -0,0 +1,20 @@
1
+ import { RequiredValidation } from '../../commonTypes.js';
2
+
3
+ /**
4
+ * @description Asserts that a string value contains at least one digit.
5
+ * @returns {RequiredValidation} A validation function that takes a received string and an exception context.
6
+ * @throws {ValidationError} if the received value does not contain at least one digit.
7
+ * @example
8
+ * const schema = string().custom(atLeastOneDigit());
9
+ * parseOrFail(schema, 'abc123'); // Valid
10
+ * parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'
11
+ *
12
+ * @translation Error Translation Key = 's:atLeastOneDigit'
13
+ */
14
+ declare const atLeastOneDigit: {
15
+ (): RequiredValidation;
16
+ key: string;
17
+ message: string;
18
+ };
19
+
20
+ export { atLeastOneDigit };
@@ -0,0 +1,21 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk5BVJ6YMHjs = require('../../chunk-5BVJ6YMH.js');
4
+
5
+
6
+ var _chunkWK4O3HH4js = require('../../chunk-WK4O3HH4.js');
7
+
8
+ // src/asserts/string/atLeastOneDigit.ts
9
+ var digitRegExp = /\d/;
10
+ var atLeastOneDigitErrorMessage = "The received value does not contain at least one digit";
11
+ var atLeastOneDigitErrorKey = "s:atLeastOneDigit";
12
+ var atLeastOneDigit = () => (received, ctx) => {
13
+ if (!digitRegExp.test(received)) _chunkWK4O3HH4js.guardException.call(void 0, "at least one digit", received, ctx, atLeastOneDigitErrorKey);
14
+ };
15
+ atLeastOneDigit.key = atLeastOneDigitErrorKey;
16
+ atLeastOneDigit.message = atLeastOneDigitErrorMessage;
17
+ _chunk5BVJ6YMHjs.setToDefaultLocale.call(void 0, atLeastOneDigit);
18
+
19
+
20
+ exports.atLeastOneDigit = atLeastOneDigit;
21
+ //# sourceMappingURL=atLeastOneDigit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["/home/runner/work/bguard/bguard/lib/asserts/string/atLeastOneDigit.js","../../../src/asserts/string/atLeastOneDigit.ts"],"names":[],"mappings":"AAAA;AACE;AACF,0DAAgC;AAChC;AACE;AACF,0DAAgC;AAChC;AACA;ACHA,IAAM,YAAA,EAAc,IAAA;AACpB,IAAM,4BAAA,EAA8B,wDAAA;AACpC,IAAM,wBAAA,EAA0B,mBAAA;AAazB,IAAM,gBAAA,EAAkB,CAAA,EAAA,GAA0B,CAAC,QAAA,EAAkB,GAAA,EAAA,GAA0B;AACpG,EAAA,GAAA,CAAI,CAAC,WAAA,CAAY,IAAA,CAAK,QAAQ,CAAA,EAAG,6CAAA,oBAAe,EAAsB,QAAA,EAAU,GAAA,EAAK,uBAAuB,CAAA;AAC9G,CAAA;AAEA,eAAA,CAAgB,IAAA,EAAM,uBAAA;AACtB,eAAA,CAAgB,QAAA,EAAU,2BAAA;AAC1B,iDAAA,eAAkC,CAAA;ADRlC;AACE;AACF,0CAAC","file":"/home/runner/work/bguard/bguard/lib/asserts/string/atLeastOneDigit.js","sourcesContent":[null,"import { ExceptionContext, RequiredValidation } from '../../commonTypes';\nimport { guardException } from '../../exceptions';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst digitRegExp = /\\d/;\nconst atLeastOneDigitErrorMessage = 'The received value does not contain at least one digit';\nconst atLeastOneDigitErrorKey = 's:atLeastOneDigit';\n\n/**\n * @description Asserts that a string value contains at least one digit.\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 digit.\n * @example\n * const schema = string().custom(atLeastOneDigit());\n * parseOrFail(schema, 'abc123'); // Valid\n * parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'\n *\n * @translation Error Translation Key = 's:atLeastOneDigit'\n */\nexport const atLeastOneDigit = (): RequiredValidation => (received: string, ctx: ExceptionContext) => {\n if (!digitRegExp.test(received)) guardException('at least one digit', received, ctx, atLeastOneDigitErrorKey);\n};\n\natLeastOneDigit.key = atLeastOneDigitErrorKey;\natLeastOneDigit.message = atLeastOneDigitErrorMessage;\nsetToDefaultLocale(atLeastOneDigit);\n"]}
@@ -0,0 +1,21 @@
1
+ import {
2
+ setToDefaultLocale
3
+ } from "../../chunk-3HTYHE2X.mjs";
4
+ import {
5
+ guardException
6
+ } from "../../chunk-7XFLH6R2.mjs";
7
+
8
+ // src/asserts/string/atLeastOneDigit.ts
9
+ var digitRegExp = /\d/;
10
+ var atLeastOneDigitErrorMessage = "The received value does not contain at least one digit";
11
+ var atLeastOneDigitErrorKey = "s:atLeastOneDigit";
12
+ var atLeastOneDigit = () => (received, ctx) => {
13
+ if (!digitRegExp.test(received)) guardException("at least one digit", received, ctx, atLeastOneDigitErrorKey);
14
+ };
15
+ atLeastOneDigit.key = atLeastOneDigitErrorKey;
16
+ atLeastOneDigit.message = atLeastOneDigitErrorMessage;
17
+ setToDefaultLocale(atLeastOneDigit);
18
+ export {
19
+ atLeastOneDigit
20
+ };
21
+ //# sourceMappingURL=atLeastOneDigit.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/asserts/string/atLeastOneDigit.ts"],"sourcesContent":["import { ExceptionContext, RequiredValidation } from '../../commonTypes';\nimport { guardException } from '../../exceptions';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst digitRegExp = /\\d/;\nconst atLeastOneDigitErrorMessage = 'The received value does not contain at least one digit';\nconst atLeastOneDigitErrorKey = 's:atLeastOneDigit';\n\n/**\n * @description Asserts that a string value contains at least one digit.\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 digit.\n * @example\n * const schema = string().custom(atLeastOneDigit());\n * parseOrFail(schema, 'abc123'); // Valid\n * parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'\n *\n * @translation Error Translation Key = 's:atLeastOneDigit'\n */\nexport const atLeastOneDigit = (): RequiredValidation => (received: string, ctx: ExceptionContext) => {\n if (!digitRegExp.test(received)) guardException('at least one digit', received, ctx, atLeastOneDigitErrorKey);\n};\n\natLeastOneDigit.key = atLeastOneDigitErrorKey;\natLeastOneDigit.message = atLeastOneDigitErrorMessage;\nsetToDefaultLocale(atLeastOneDigit);\n"],"mappings":";;;;;;;;AAIA,IAAM,cAAc;AACpB,IAAM,8BAA8B;AACpC,IAAM,0BAA0B;AAazB,IAAM,kBAAkB,MAA0B,CAAC,UAAkB,QAA0B;AACpG,MAAI,CAAC,YAAY,KAAK,QAAQ,EAAG,gBAAe,sBAAsB,UAAU,KAAK,uBAAuB;AAC9G;AAEA,gBAAgB,MAAM;AACtB,gBAAgB,UAAU;AAC1B,mBAAmB,eAAe;","names":[]}