kanun 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Toneflix, LLC. and Arcstack Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # Kanun
2
+
3
+ [![Validation Package Version][i1]][l1]
4
+ [![Downloads][d1]][l1]
5
+ [![Tests][tei]][tel]
6
+ [![License][lini]][linl]
7
+
8
+ Lightweight framework-agnostic and TypeScript-first validation library. Provides a rich set of built-in rules, custom rule support, nested data validation, conditional rules, localized error messages, and a flexible API for validating complex data structures in Node.js applications.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install kanun
14
+ ```
15
+
16
+ ## Features
17
+
18
+ - Rule-based validation — Supports common rules like required, min, max, email, url, numeric, boolean, in, regex, etc.
19
+ - Nested data validation — Dot notation for nested objects (user.email, items.\*.price).
20
+ - Custom error messages — Per-rule and per-field message overrides.
21
+ - Batch validation — Validate multiple datasets or groups in one call.
22
+ - Conditional validation — Rules that only apply when other fields meet conditions (required_if, sometimes, exclude_unless).
23
+ - Implicit rules — Rules that run even when attributes are missing (e.g., accepted, required).
24
+
25
+ - Custom rules — Define user-provided validation rules as functions or classes.
26
+ - Async rules — Support for async validation (e.g., checking uniqueness in a database).
27
+ - Attribute sanitization — Optional transformation (e.g., trimming, normalizing case) before validation.
28
+ - Dynamic rule sets — Rules can be generated at runtime (e.g., based on user roles).
29
+ - Dependent rules — Rules that reference other fields dynamically.
30
+
31
+ - Localized error messages — Built-in support for localization and i18n message templates.
32
+ - Structured errors — Validation errors returned as structured objects or flat key–message pairs.
33
+ - Fail-fast mode — Option to stop at the first failure or collect all errors.
34
+ - Human-readable summaries — Helper for formatting readable validation reports.
35
+
36
+ - TypeScript-first design — Full type inference for rules, messages, and validated data.
37
+ - Chainable API — Optional fluent syntax for building validators.
38
+
39
+ ## Usage
40
+
41
+ ### Quick Start
42
+
43
+ ```ts
44
+ import { Validator } from 'kanun';
45
+
46
+ const validator = new Validator(
47
+ { email: 'john@example.com', age: 20 },
48
+ { email: 'required|email', age: 'required|numeric|min:18' },
49
+ );
50
+
51
+ const passes = await validator.passes();
52
+
53
+ if (!passes) {
54
+ console.log(validator.errors().all());
55
+ }
56
+ ```
57
+
58
+ ### Throwing Validation Errors
59
+
60
+ ```ts
61
+ import { Validator, ValidationException } from 'kanun';
62
+
63
+ const validator = Validator.make({ email: '' }, { email: 'required|email' });
64
+
65
+ try {
66
+ const data = await validator.validate();
67
+ console.log(data);
68
+ } catch (error) {
69
+ if (error instanceof ValidationException) {
70
+ console.log(error.errors());
71
+ }
72
+ }
73
+ ```
74
+
75
+ ### Custom Messages
76
+
77
+ ```ts
78
+ const validator = new Validator(
79
+ { email: '' },
80
+ { email: 'required|email' },
81
+ {
82
+ 'email.required': 'Email is required.',
83
+ 'email.email': 'Email must be valid.',
84
+ },
85
+ );
86
+ ```
87
+
88
+ ### Nested and Array Validation
89
+
90
+ ```ts
91
+ const validator = new Validator(
92
+ {
93
+ user: { name: { first: 'John', last: '' } },
94
+ users: [{ email: 'good@example.com' }, { email: 'bad' }],
95
+ },
96
+ {
97
+ 'user.name.first': 'required|min:2',
98
+ 'user.name.last': 'required|min:2',
99
+ 'users.*.email': 'required|email',
100
+ },
101
+ );
102
+ ```
103
+
104
+ ### Custom Rule Class
105
+
106
+ ```ts
107
+ import { ValidationRule, Validator } from 'kanun';
108
+
109
+ class StartsWithKanun extends ValidationRule {
110
+ validate(
111
+ attribute: string,
112
+ value: any,
113
+ fail: (message: string) => void,
114
+ ): void {
115
+ if (typeof value !== 'string' || !value.startsWith('kanun')) {
116
+ fail(`The ${attribute} must start with kanun.`);
117
+ }
118
+ }
119
+ }
120
+
121
+ const validator = new Validator(
122
+ { slug: 'kanun' },
123
+ { slug: ['required', new StartsWithKanun()] },
124
+ );
125
+ ```
126
+
127
+ ### Database-backed `exists` and `unique`
128
+
129
+ ```ts
130
+ import { IDatabaseDriver, Validator } from 'kanun';
131
+
132
+ class AppDatabaseDriver extends IDatabaseDriver {
133
+ async exists({ table, column, value, ignore }) {
134
+ const row = await db.table(table).where(column, value).first();
135
+ if (!row) return false;
136
+ if (ignore != null && String(row.id) === String(ignore)) return false;
137
+ return true;
138
+ }
139
+ }
140
+
141
+ const driver = new AppDatabaseDriver();
142
+
143
+ Validator.useDatabase(driver);
144
+
145
+ const validator = new Validator(
146
+ { username: 'legacy' },
147
+ { username: 'unique:users,username' },
148
+ );
149
+ ```
150
+
151
+ For the complete guide and API reference, visit the docs site: https://arcstack-hq.github.io/kanun
152
+
153
+ ## Code of Conduct
154
+
155
+ In order to ensure that the Kanun community is welcoming to all, please review and abide by the [Code of Conduct](https://arcstack-hq.github.io/code-of-conduct).
156
+
157
+ ## Security Vulnerabilities
158
+
159
+ If you discover a security vulnerability within Kanun, please send an e-mail to Legacy via hi@toneflix.net. All security vulnerabilities will be promptly addressed.
160
+
161
+ ## License
162
+
163
+ Kanun and all Arcstack packages are open source and licensed under the [MIT license](LICENSE).
164
+
165
+ [i1]: https://img.shields.io/npm/v/%40kanun?style=flat-square&label=kanun&color=%230970ce
166
+ [l1]: https://www.npmjs.com/package/kanun
167
+ [d1]: https://img.shields.io/npm/dt/%40kanun?style=flat-square&label=Downloads&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2F%40kanun
168
+ [linl]: https://github.com/arcstack-hq/kanun/blob/main/LICENSE
169
+ [lini]: https://img.shields.io/github/license/arcstack-hq/kanun
170
+ [tel]: https://github.com/arcstack-hq/kanun/actions/workflows/ci.yml
171
+ [tei]: https://github.com/arcstack-hq/kanun/actions/workflows/ci.yml/badge.svg