@kklab/fortress-validator 1.0.9

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 (67) hide show
  1. package/README.md +202 -0
  2. package/dist/FieldValidator.d.ts +272 -0
  3. package/dist/FormValidator.d.ts +19 -0
  4. package/dist/index.d.ts +3 -0
  5. package/dist/index.js +919 -0
  6. package/dist/index.umd.js +1 -0
  7. package/dist/locales/en.d.ts +3 -0
  8. package/dist/locales/index.d.ts +3 -0
  9. package/dist/locales/zh-TW.d.ts +3 -0
  10. package/dist/rules/accepted.d.ts +3 -0
  11. package/dist/rules/alpha.d.ts +3 -0
  12. package/dist/rules/alphaDash.d.ts +3 -0
  13. package/dist/rules/alphaDashDot.d.ts +3 -0
  14. package/dist/rules/alphaNum.d.ts +3 -0
  15. package/dist/rules/array.d.ts +3 -0
  16. package/dist/rules/ascii.d.ts +3 -0
  17. package/dist/rules/between.d.ts +7 -0
  18. package/dist/rules/betweenLength.d.ts +7 -0
  19. package/dist/rules/boolean.d.ts +3 -0
  20. package/dist/rules/containsAll.d.ts +6 -0
  21. package/dist/rules/containsAny.d.ts +6 -0
  22. package/dist/rules/declined.d.ts +3 -0
  23. package/dist/rules/different.d.ts +7 -0
  24. package/dist/rules/distinct.d.ts +3 -0
  25. package/dist/rules/domain.d.ts +3 -0
  26. package/dist/rules/email.d.ts +3 -0
  27. package/dist/rules/endsWith.d.ts +6 -0
  28. package/dist/rules/equals.d.ts +6 -0
  29. package/dist/rules/file.d.ts +3 -0
  30. package/dist/rules/fileBetweenSize.d.ts +7 -0
  31. package/dist/rules/fileMaxSize.d.ts +6 -0
  32. package/dist/rules/fileMinSize.d.ts +6 -0
  33. package/dist/rules/fileSize.d.ts +6 -0
  34. package/dist/rules/http.d.ts +3 -0
  35. package/dist/rules/https.d.ts +3 -0
  36. package/dist/rules/in.d.ts +6 -0
  37. package/dist/rules/index.d.ts +3 -0
  38. package/dist/rules/integer.d.ts +3 -0
  39. package/dist/rules/json.d.ts +3 -0
  40. package/dist/rules/length.d.ts +6 -0
  41. package/dist/rules/lowercase.d.ts +3 -0
  42. package/dist/rules/max.d.ts +6 -0
  43. package/dist/rules/maxLength.d.ts +6 -0
  44. package/dist/rules/min.d.ts +6 -0
  45. package/dist/rules/minLength.d.ts +6 -0
  46. package/dist/rules/notEquals.d.ts +6 -0
  47. package/dist/rules/notIn.d.ts +6 -0
  48. package/dist/rules/number.d.ts +3 -0
  49. package/dist/rules/numeric.d.ts +3 -0
  50. package/dist/rules/regex.d.ts +6 -0
  51. package/dist/rules/required.d.ts +3 -0
  52. package/dist/rules/same.d.ts +7 -0
  53. package/dist/rules/size.d.ts +6 -0
  54. package/dist/rules/startsWith.d.ts +6 -0
  55. package/dist/rules/string.d.ts +3 -0
  56. package/dist/rules/stringBetweenLength.d.ts +7 -0
  57. package/dist/rules/stringLength.d.ts +6 -0
  58. package/dist/rules/stringMaxLength.d.ts +6 -0
  59. package/dist/rules/stringMinLength.d.ts +6 -0
  60. package/dist/rules/unique.d.ts +7 -0
  61. package/dist/rules/uppercase.d.ts +3 -0
  62. package/dist/rules/url.d.ts +3 -0
  63. package/dist/types/Conditions.d.ts +4 -0
  64. package/dist/types/FieldValidatorArguments.d.ts +9 -0
  65. package/dist/types/FormValidatorArguments.d.ts +7 -0
  66. package/dist/types/index.d.ts +4 -0
  67. package/package.json +65 -0
package/README.md ADDED
@@ -0,0 +1,202 @@
1
+ # Fortress Validator
2
+
3
+ A powerful and flexible form validation library, built to validate inputs through method chaining, with support for custom plugins.
4
+
5
+ ## Getting Started
6
+
7
+ ### Using with ES Modules
8
+
9
+ To get started with ES Modules, simply import the module and use it in your code:
10
+
11
+ ```js
12
+ import { FormValidator } from '@kklab/fortress-validator';
13
+
14
+ const validator = new FormValidator();
15
+ ```
16
+
17
+ ### Using with UMD Modules
18
+
19
+ Alternatively, if you're using UMD modules, include the script in your HTML file and use it in your code:
20
+
21
+ ```html
22
+ <script src="https://unpkg.com/@kklab/fortress-validator/dist/index.umd.js"></script>
23
+ <script>
24
+ const validator = new window.Fortress.FormValidator();
25
+ </script>
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ### Basic Validation
31
+
32
+ The `defineField` method sets up a field for validation, allowing you to chain validation rules like `required` and others to validate the input.
33
+
34
+ ```js
35
+ document.querySelector('input').addEventListener('input', (e) => {
36
+ const result = validator
37
+ .defineField('Input')
38
+ .required()
39
+ .alphaDash()
40
+ .validate(e.target.value);
41
+
42
+ console.log(result);
43
+ });
44
+ ```
45
+
46
+ ### Conditional Validation
47
+
48
+ The `when` method allows you to conditionally apply validation rules like `required` and others based on specific conditions.
49
+
50
+ ```js
51
+ const result = validator
52
+ .defineField('Input')
53
+ .when({
54
+ alphaDash: false,
55
+ })
56
+ .required()
57
+ .alphaDash()
58
+ .validate('@');
59
+
60
+ // Result:
61
+ // true
62
+ ```
63
+
64
+ ## Available Rules
65
+
66
+ | Name | Description |
67
+ | --- | --- |
68
+ | `accepted` | Passes if the field's value is considered accepted (i.e., "y", "yes", "on", "1", "true"). |
69
+ | `after` | Passes if the field's value is a date that occurs after the specified date. |
70
+ | `alpha` | Passes if the field's value contains only letters. |
71
+ | `alphaDash` | Passes if the field's value contains only letters, numbers, dashes and underscores. |
72
+ | `alphaDashDot` | Passes if the field's value contains only letters, numbers, dashes, underscores and dots. |
73
+ | `alphaNum` | Passes if the field's value contains only letters and numbers. |
74
+ | `array` | Passes if the field's value is an array. |
75
+ | `ascii` | Passes if the field's value contains only ASCII characters and symbols. |
76
+ | `before` | Passes if the field's value is a date that occurs before the specified date. |
77
+ | `between` | Passes if the field's value is between the specified minimum and maximum values. |
78
+ | `betweenLength` | Passes if the field's value is between the specified minimum and maximum lengths. |
79
+ | `boolean` | Passes if the field's value is a boolean. |
80
+ | `containsAll` | Passes if the field's value contains all the specified values. |
81
+ | `containsAny` | Passes if the field's value contains at least one of the specified values. |
82
+ | `date` | Passes if the field's value matches the specified date format. |
83
+ | `declined` | Passes if the field's value is considered declined (i.e., "n", "no", "off", "0", "false"). |
84
+ | `different` | Passes if the field's value is different from the specified value in the given field. |
85
+ | `distinct` | Passes if all the items in the array field's value are unique. |
86
+ | `domain` | Passes if the field's value is a valid domain. |
87
+ | `email` | Passes if the field's value is a valid email address. |
88
+ | `endsWith` | Passes if the field's value ends with the specified value. |
89
+ | `equals` | Passes if the field's value is equal to the specified value. |
90
+ | `file` | Passes if the field's value is a file. |
91
+ | `fileBetweenSize` | Passes if the field's value is between the specified minimum and maximum file sizes. |
92
+ | `fileMaxSize` | Passes if the field's value is not greater than the specified maximum file size. |
93
+ | `fileMinSize` | Passes if the field's value is at least the specified minimum file size. |
94
+ | `fileSize` | Passes if the field's value matches the specified file size. |
95
+ | `http` | Passes if the field's value starts with "http://" or "https://". |
96
+ | `https` | Passes if the field's value starts with "https://". |
97
+ | `in` | Passes if the field's value is one of the specified values. |
98
+ | `integer` | Passes if the field's value is an integer. |
99
+ | `iso8601` | Passes if the field's value is a valid ISO 8601 date. |
100
+ | `json` | Passes if the field's value is a valid JSON string. |
101
+ | `jsonSchema` | Passes if the field's value matches the specified JSON schema. |
102
+ | `length` | Passes if the field's value matches the specified length. |
103
+ | `lowercase` | Passes if the field's value contains only lowercase characters. |
104
+ | `max` | Passes if the field's value is not greater than the specified maximum. |
105
+ | `maxLength` | Passes if the field's value is not greater than the specified maximum length. |
106
+ | `min` | Passes if the field's value is at least the specified minimum. |
107
+ | `minLength` | Passes if the field's value is at least the specified minimum length. |
108
+ | `notEquals` | Passes if the field's value is not equal to the specified value. |
109
+ | `notIn` | Passes if the field's value is not one of the specified values. |
110
+ | `number` | Passes if the field's value is a number. |
111
+ | `numeric` | Passes if the field's value contains only numeric characters. |
112
+ | `regex` | Passes if the field's value matches the specified regular expression. |
113
+ | `required` | Passes if the field's value is not empty. |
114
+ | `requiredWhen` | Passes if the field's value is not empty when the specified condition is true. |
115
+ | `same` | Passes if the field's value is the same as the specified value in the given field. |
116
+ | `size` | Passes if the field's value matches the specified size. |
117
+ | `startsWith` | Passes if the field's value starts with the specified value. |
118
+ | `string` | Passes if the field's value is a string. |
119
+ | `stringBetweenLength` | Passes if the field's value is between the specified minimum and maximum string lengths. |
120
+ | `stringLength` | Passes if the field's value matches the specified string length. |
121
+ | `stringMaxLength` | Passes if the field's value is not greater than the specified maximum string length. |
122
+ | `stringMinLength` | Passes if the field's value is at least the specified minimum string length. |
123
+ | `unique` | Passes if the field's value contains only unique items, with optional ignored values. |
124
+ | `uppercase` | Passes if the field's value contains only uppercase characters. |
125
+ | `url` | Passes if the field's value is a valid URL. |
126
+
127
+ ## Available Methods
128
+
129
+ | Name | Description |
130
+ | --- | --- |
131
+ | `collect` | Collects the rule functions. |
132
+ | `validate` | Validates the field's value. |
133
+ | `apply` | Applies the specified rule with the given arguments, useful for applying custom rules. |
134
+ | `when` | Determines whether to apply or skip validation based on the provided conditions. |
135
+
136
+ ## Available Plugins
137
+
138
+ - [fortress-validator/plugin-date](https://github.com/fortress-validator/plugin-date)
139
+ - [fortress-validator/plugin-json-schema](https://github.com/fortress-validator/plugin-json-schema)
140
+
141
+ ## Custom Plugin
142
+
143
+ A custom plugin is composed of `rules` and `locales`, allowing you to define custom validation logic and locale-specific messages.
144
+
145
+ ```js
146
+ const validator = new FormValidator({
147
+ plugins: [
148
+ {
149
+ rules: {
150
+ multipleOf: (({ factor }: { factor: number }) => (input: unknown) => Number(input) % factor === 0) as Rule<unknown>,
151
+ },
152
+ locales: {
153
+ en: {
154
+ multipleOf: (field, args) => {
155
+ const { factor } = args as { factor: number };
156
+ return `The ${field} field must be a multiple of ${factor}.`;
157
+ },
158
+ },
159
+ },
160
+ },
161
+ ],
162
+ });
163
+
164
+ const result = validator
165
+ .defineField('Input')
166
+ .apply('multipleOf', { factor: 2 })
167
+ .validate(1);
168
+
169
+ // Result:
170
+ // The input field must be a multiple of 2.
171
+ ```
172
+
173
+ ## Development
174
+
175
+ To start a local development server, run:
176
+
177
+ ```bash
178
+ npm run dev
179
+ ```
180
+
181
+ ## Testing
182
+
183
+ To run the tests for this package, run:
184
+
185
+ ```bash
186
+ npm run test
187
+ ```
188
+
189
+ ## Contributors
190
+
191
+ <a href="https://github.com/memochou1993">
192
+ <img src="https://github.com/memochou1993.png" width="60px;" alt="memochou1993" />
193
+ </a>
194
+ <a href="https://github.com/clover180120">
195
+ <img src="https://github.com/clover180120.png" width="60px;" alt="clover180120" />
196
+ </a>
197
+ <a href="https://github.com/Joyining">
198
+ <img src="https://github.com/Joyining.png" width="60px;" alt="Joyining" />
199
+ </a>
200
+ <a href="https://github.com/BelleShih">
201
+ <img src="https://github.com/BelleShih.png" width="60px;" alt="BelleShih" />
202
+ </a>
@@ -0,0 +1,272 @@
1
+ import { Message, Messages, Rule, RuleArguments, RuleFunction } from '@kklab/fortress-validator-types';
2
+ import { Conditions, FieldValidatorArguments } from './types';
3
+ declare class FieldValidator {
4
+ private name;
5
+ private locale;
6
+ private fallbackLocale;
7
+ private locales;
8
+ private rules;
9
+ private ruleFunctions;
10
+ private conditions;
11
+ private shouldSkip;
12
+ constructor({ name, locale, fallbackLocale, locales, rules, }: FieldValidatorArguments);
13
+ get formattedName(): string;
14
+ get messages(): Messages;
15
+ get fallbackMessages(): Messages;
16
+ get mandatoryRules(): string[];
17
+ getMessage(ruleName: string): Message;
18
+ getRule(name: string): Rule<unknown>;
19
+ private buildRuleFunction;
20
+ private buildRuleFunctionMessage;
21
+ private pushRuleFunction;
22
+ getRuleFunctions(): RuleFunction[];
23
+ /**
24
+ * Collects the rule functions.
25
+ */
26
+ collect(): RuleFunction[];
27
+ /**
28
+ * Validates the field's value.
29
+ */
30
+ validate(value: unknown): boolean | string;
31
+ /**
32
+ * Applies the specified rule with the given arguments.
33
+ */
34
+ apply(ruleName: string, args?: RuleArguments): this;
35
+ /**
36
+ * Passes if the field's value is considered accepted (i.e., "y", "yes", "on", "1", "true").
37
+ */
38
+ accepted(): this;
39
+ /**
40
+ * Passes if the field's value is a date that occurs after the specified date.
41
+ */
42
+ after(date: string, format: string, displayFormat?: string, strict?: boolean): this;
43
+ /**
44
+ * Passes if the field's value contains only letters.
45
+ */
46
+ alpha(): this;
47
+ /**
48
+ * Passes if the field's value contains only letters, numbers, dashes and underscores.
49
+ */
50
+ alphaDash(): this;
51
+ /**
52
+ * Passes if the field's value contains only letters, numbers, dashes, underscores and dots.
53
+ */
54
+ alphaDashDot(): this;
55
+ /**
56
+ * Passes if the field's value contains only letters and numbers.
57
+ */
58
+ alphaNum(): this;
59
+ /**
60
+ * Passes if the field's value is an array.
61
+ */
62
+ array(): this;
63
+ /**
64
+ * Passes if the field's value contains only ASCII characters and symbols.
65
+ */
66
+ ascii(): this;
67
+ /**
68
+ * Passes if the field's value is a date that occurs before the specified date.
69
+ */
70
+ before(date: string, format: string, displayFormat?: string, strict?: boolean): this;
71
+ /**
72
+ * Passes if the field's value is between the specified minimum and maximum values.
73
+ */
74
+ between(min: number, max: number): this;
75
+ /**
76
+ * Passes if the field's value is between the specified minimum and maximum lengths.
77
+ */
78
+ betweenLength(min: number, max: number): this;
79
+ /**
80
+ * Passes if the field's value is a boolean.
81
+ */
82
+ boolean(): this;
83
+ /**
84
+ * Passes if the field's value contains all the specified values.
85
+ */
86
+ containsAll(values: unknown[]): this;
87
+ /**
88
+ * Passes if the field's value contains at least one of the specified values.
89
+ */
90
+ containsAny(values: unknown[]): this;
91
+ /**
92
+ * Passes if the field's value matches the specified date format.
93
+ */
94
+ date(format: string, strict?: boolean): this;
95
+ /**
96
+ * Passes if the field's value is considered declined (i.e., "n", "no", "off", "0", "false").
97
+ */
98
+ declined(): this;
99
+ /**
100
+ * Passes if the field's value is different from the specified value in the given field.
101
+ */
102
+ different(field: string, value: unknown): this;
103
+ /**
104
+ * Passes if all the items in the array field's value are unique.
105
+ */
106
+ distinct(): this;
107
+ /**
108
+ * Passes if the field's value is a valid domain.
109
+ */
110
+ domain(): this;
111
+ /**
112
+ * Passes if the field's value is a valid email address.
113
+ */
114
+ email(): this;
115
+ /**
116
+ * Passes if the field's value ends with the specified value.
117
+ */
118
+ endsWith(value: string): this;
119
+ /**
120
+ * Passes if the field's value is equal to the specified value.
121
+ */
122
+ equals(value: unknown): this;
123
+ /**
124
+ * Passes if the field's value is a file.
125
+ */
126
+ file(): this;
127
+ /**
128
+ * Passes if the field's value is between the specified minimum and maximum file sizes.
129
+ */
130
+ fileBetweenSize(min: number, max: number): this;
131
+ /**
132
+ * Passes if the field's value is not greater than the specified maximum file size.
133
+ */
134
+ fileMaxSize(size: number): this;
135
+ /**
136
+ * Passes if the field's value is at least the specified minimum file size.
137
+ */
138
+ fileMinSize(size: number): this;
139
+ /**
140
+ * Passes if the field's value matches the specified file size.
141
+ */
142
+ fileSize(size: number): this;
143
+ /**
144
+ * Passes if the field's value starts with "http://" or "https://".
145
+ */
146
+ http(): this;
147
+ /**
148
+ * Passes if the field's value starts with "https://".
149
+ */
150
+ https(): this;
151
+ /**
152
+ * Passes if the field's value is one of the specified values.
153
+ */
154
+ in(values: unknown[]): this;
155
+ /**
156
+ * Passes if the field's value is an integer.
157
+ */
158
+ integer(): this;
159
+ /**
160
+ * Passes if the field's value is a valid ISO 8601 date.
161
+ */
162
+ iso8601(): this;
163
+ /**
164
+ * Passes if the field's value is a valid JSON string.
165
+ */
166
+ json(): this;
167
+ /**
168
+ * Passes if the field's value matches the specified JSON schema.
169
+ */
170
+ jsonSchema(schema: Record<string, unknown>): this;
171
+ /**
172
+ * Passes if the field's value matches the specified length.
173
+ */
174
+ length(length: number): this;
175
+ /**
176
+ * Passes if the field's value contains only lowercase characters.
177
+ */
178
+ lowercase(): this;
179
+ /**
180
+ * Passes if the field's value is not greater than the specified maximum.
181
+ */
182
+ max(max: number): this;
183
+ /**
184
+ * Passes if the field's value is not greater than the specified maximum length.
185
+ */
186
+ maxLength(length: number): this;
187
+ /**
188
+ * Passes if the field's value is at least the specified minimum.
189
+ */
190
+ min(min: number): this;
191
+ /**
192
+ * Passes if the field's value is at least the specified minimum length.
193
+ */
194
+ minLength(length: number): this;
195
+ /**
196
+ * Passes if the field's value is not equal to the specified value.
197
+ */
198
+ notEquals(value: unknown): this;
199
+ /**
200
+ * Passes if the field's value is not one of the specified values.
201
+ */
202
+ notIn(values: string[]): this;
203
+ /**
204
+ * Passes if the field's value is a number.
205
+ */
206
+ number(): this;
207
+ /**
208
+ * Passes if the field's value contains only numeric characters.
209
+ */
210
+ numeric(): this;
211
+ /**
212
+ * Passes if the field's value matches the specified regular expression.
213
+ */
214
+ regex(expression: RegExp): this;
215
+ /**
216
+ * Passes if the field's value is not empty.
217
+ */
218
+ required(): this;
219
+ /**
220
+ * Passes if the field's value is not empty when the specified condition is true.
221
+ */
222
+ requiredWhen(condition: boolean): this;
223
+ /**
224
+ * Passes if the field's value is the same as the specified value in the given field.
225
+ */
226
+ same(field: string, value: unknown): this;
227
+ /**
228
+ * Passes if the field's value matches the specified size.
229
+ */
230
+ size(size: number): this;
231
+ /**
232
+ * Passes if the field's value starts with the specified value.
233
+ */
234
+ startsWith(value: string): this;
235
+ /**
236
+ * Passes if the field's value is a string.
237
+ */
238
+ string(): this;
239
+ /**
240
+ * Passes if the field's value is between the specified minimum and maximum string lengths.
241
+ */
242
+ stringBetweenLength(min: number, max: number): this;
243
+ /**
244
+ * Passes if the field's value matches the specified string length.
245
+ */
246
+ stringLength(length: number): this;
247
+ /**
248
+ * Passes if the field's value is not greater than the specified maximum string length.
249
+ */
250
+ stringMaxLength(length: number): this;
251
+ /**
252
+ * Passes if the field's value is at least the specified minimum string length.
253
+ */
254
+ stringMinLength(length: number): this;
255
+ /**
256
+ * Passes if the field's value contains only unique items, with optional ignored values.
257
+ */
258
+ unique(values: string[], ignored?: unknown[] | unknown): this;
259
+ /**
260
+ * Passes if the field's value contains only uppercase characters.
261
+ */
262
+ uppercase(): this;
263
+ /**
264
+ * Passes if the field's value is a valid URL.
265
+ */
266
+ url(): this;
267
+ /**
268
+ * Determines whether to apply or skip validation based on the provided conditions.
269
+ */
270
+ when(conditions: boolean | Conditions): this;
271
+ }
272
+ export default FieldValidator;
@@ -0,0 +1,19 @@
1
+ import { Locales, Plugin, Rules } from '@kklab/fortress-validator-types';
2
+ import { default as FieldValidator } from './FieldValidator';
3
+ import { FormValidatorArguments } from './types';
4
+ declare class FormValidator {
5
+ private locale;
6
+ private fallbackLocale;
7
+ private locales;
8
+ private rules;
9
+ constructor({ fallbackLocale, locale, plugins, }?: FormValidatorArguments);
10
+ getLocale(): string;
11
+ getFallbackLocale(): string;
12
+ setLocale(locale: string): this;
13
+ setFallbackLocale(locale: string): this;
14
+ defineField(name: string): FieldValidator;
15
+ registerLocales(locales: Locales): this;
16
+ registerRules(rules: Rules): this;
17
+ registerPlugin(plugin: Plugin): this;
18
+ }
19
+ export default FormValidator;
@@ -0,0 +1,3 @@
1
+ import { default as FieldValidator } from './FieldValidator';
2
+ import { default as FormValidator } from './FormValidator';
3
+ export { FieldValidator, FormValidator, };