silentium-validation 0.0.2
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/CHANGELOG.md +23 -0
- package/LICENSE.md +7 -0
- package/README.md +160 -0
- package/beforeRelease.sh +11 -0
- package/commitizen.cjs +50 -0
- package/dist/silentium_validation.cjs +85 -0
- package/dist/silentium_validation.cjs.map +1 -0
- package/dist/silentium_validation.d.ts +66 -0
- package/dist/silentium_validation.js +76 -0
- package/dist/silentium_validation.js.map +1 -0
- package/dist/silentium_validation.min.js +1 -0
- package/dist/silentium_validation.min.mjs +2 -0
- package/dist/silentium_validation.min.mjs.map +1 -0
- package/dist/silentium_validation.mjs +76 -0
- package/dist/silentium_validation.mjs.map +1 -0
- package/eslint.config.mjs +70 -0
- package/package.json +61 -0
- package/rollup.config.js +66 -0
- package/src/index.ts +8 -0
- package/src/models/Validated.test.ts +16 -0
- package/src/models/Validated.ts +11 -0
- package/src/models/ValidationErrors.test.ts +127 -0
- package/src/models/ValidationErrors.ts +51 -0
- package/src/models/ValidationErrorsHappened.test.ts +16 -0
- package/src/models/ValidationErrorsHappened.ts +10 -0
- package/src/models/ValidationErrorsSummary.test.ts +21 -0
- package/src/models/ValidationErrorsSummary.ts +8 -0
- package/src/models/ValidationErrorsTouched.test.ts +32 -0
- package/src/models/ValidationErrorsTouched.ts +21 -0
- package/src/models/ValidationItems.test.ts +94 -0
- package/src/models/ValidationItems.ts +24 -0
- package/src/rules.ts +9 -0
- package/src/types.ts +11 -0
- package/tsconfig.json +30 -0
- package/vitest.config.js +21 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
### 0.0.2 (2025-12-02)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* main logic ([4d91ff7](https://github.com/silentium-lab/silentium-validation/commit/4d91ff7c9f610b9eabbad12bd2cc3ac9f40eea26))
|
|
11
|
+
* **main:** all main logic done ([050d1ba](https://github.com/silentium-lab/silentium-validation/commit/050d1ba6147cb17949154017612a7c13d37e7b43))
|
|
12
|
+
* **main:** preparations to publication ([29088df](https://github.com/silentium-lab/silentium-validation/commit/29088df5a24a3623c48382230ded6da06e92ff9e))
|
|
13
|
+
* tests templates ([e3ebe4a](https://github.com/silentium-lab/silentium-validation/commit/e3ebe4a83b710c0a834181345d6caa49ffac2fa9))
|
|
14
|
+
* validated component ([913f1d2](https://github.com/silentium-lab/silentium-validation/commit/913f1d27b66a74b97d1a81a187a56f0c9a6b121f))
|
|
15
|
+
* Validation errors component ([e049fea](https://github.com/silentium-lab/silentium-validation/commit/e049feaa698bbbc1e3d6e97b30b9829b09e6e9b6))
|
|
16
|
+
* Validation items is just computation ([d28c2f9](https://github.com/silentium-lab/silentium-validation/commit/d28c2f9208e2b74a0b02b8b6972690cd3954451d))
|
|
17
|
+
* validation types ([1e71bc5](https://github.com/silentium-lab/silentium-validation/commit/1e71bc580dfcd8b2876aca350e0257e856d4e53f))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **main:** readme and tests config ([47a3512](https://github.com/silentium-lab/silentium-validation/commit/47a35126bb0262d35fcbfceb012626f32d683863))
|
|
23
|
+
* **main:** readme fixes ([94d38c5](https://github.com/silentium-lab/silentium-validation/commit/94d38c5d945fb61e449f813b069a87291da3413f))
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2025 Silentium Lab
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Silentium Validation
|
|
2
|
+
|
|
3
|
+
A reactive validation library built on the Silentium library for TypeScript applications. Provides asynchronous and reactive validation capabilities with a simple, composable API.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Reactive validation with asynchronous support
|
|
8
|
+
- Composable validation rules
|
|
9
|
+
- TypeScript-first design
|
|
10
|
+
- Built-in common validation rules
|
|
11
|
+
- Integration with Silentium reactive primitives
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install silentium-validation
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Basic Usage
|
|
20
|
+
|
|
21
|
+
### Validation Rules
|
|
22
|
+
|
|
23
|
+
Create validation items with rules:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { Required, Integer } from 'silentium-validation';
|
|
27
|
+
|
|
28
|
+
const form = {
|
|
29
|
+
email: '',
|
|
30
|
+
password: '',
|
|
31
|
+
};
|
|
32
|
+
const rules = {
|
|
33
|
+
email: [Required, (v) => /\S+@\S+\.\S+/.test(v) || 'Invalid email'],
|
|
34
|
+
password: [Required]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const $validationItems = Computed(ValidationItems, form, rules);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Custom Rules
|
|
41
|
+
|
|
42
|
+
Define custom validation rules:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const MinLength = (min: number) => (value: string) =>
|
|
46
|
+
value.length >= min || `Must be at least ${min} characters`;
|
|
47
|
+
|
|
48
|
+
const form = {
|
|
49
|
+
username: '',
|
|
50
|
+
age: 0,
|
|
51
|
+
password: '',
|
|
52
|
+
};
|
|
53
|
+
const rules = {
|
|
54
|
+
password: [MinLength(8)]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const $validationItems = Computed(ValidationItems, form, rules);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Running Validation
|
|
61
|
+
|
|
62
|
+
Use `ValidationErrors` to get validation results:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { ValidationErrors } from 'silentium-validation';
|
|
66
|
+
|
|
67
|
+
const $errors = ValidationErrors($validationItems);
|
|
68
|
+
const errors = await $errors;
|
|
69
|
+
|
|
70
|
+
// Result: { username: [], age: [], password: ['Must be at least 8 characters'] }
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Checking Validation Status
|
|
74
|
+
|
|
75
|
+
Use `Validated` to check if form is valid:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { Validated, Computed, LateShared } from 'silentium';
|
|
79
|
+
import { Validated } from 'silentium-validation';
|
|
80
|
+
|
|
81
|
+
const $errors = LateShared(errors);
|
|
82
|
+
const $isValid = Computed(Validated, $errors);
|
|
83
|
+
|
|
84
|
+
const isValid = await $isValid; // false if any errors exist
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## API Reference
|
|
88
|
+
|
|
89
|
+
### Types
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
type ValidationErrorType = Record<string, string[]>;
|
|
93
|
+
|
|
94
|
+
interface ValidationItem {
|
|
95
|
+
value: unknown;
|
|
96
|
+
key: string;
|
|
97
|
+
rules: ConstructorType<any, ValidationRule>[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
type ValidationRule = MaybeMessage<string | boolean>;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Functions
|
|
104
|
+
|
|
105
|
+
#### ValidationErrors(items: ValidationItem[])
|
|
106
|
+
|
|
107
|
+
Returns a `MessageType<ValidationErrorType>` containing all validation errors.
|
|
108
|
+
|
|
109
|
+
#### Validated(errors: ValidationErrorType)
|
|
110
|
+
|
|
111
|
+
Returns `true` if no validation errors exist, `false` otherwise.
|
|
112
|
+
|
|
113
|
+
### Built-in Rules
|
|
114
|
+
|
|
115
|
+
#### Required
|
|
116
|
+
|
|
117
|
+
Validates that a value is truthy.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
Required(value) // Returns true or "Field required"
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### Integer
|
|
124
|
+
|
|
125
|
+
Validates that a value is an integer.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
Integer(value) // Returns true or "Must be integer"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Advanced Usage
|
|
132
|
+
|
|
133
|
+
### Reactive Validation
|
|
134
|
+
|
|
135
|
+
Combine with Silentium's reactive primitives for real-time validation:
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { Computed, LateShared } from 'silentium';
|
|
139
|
+
import { ValidationErrors, Validated } from 'silentium-validation';
|
|
140
|
+
|
|
141
|
+
const $form = LateShared({
|
|
142
|
+
email: '',
|
|
143
|
+
password: '',
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const $validationItems = Computed(ValidationItems, $form, {
|
|
147
|
+
email: [Required, (v) => /\S+@\S+\.\S+/.test(v) || 'Invalid email'],
|
|
148
|
+
password: [Required, MinLength(8)]
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const $errors = ValidationErrors($validationItems);
|
|
152
|
+
const $isValid = Computed(Validated, $errors);
|
|
153
|
+
|
|
154
|
+
// Reactively update validation as form data changes
|
|
155
|
+
$form.use({ email: 'user@example.com', password: 'newpassword' });
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
MIT
|
package/beforeRelease.sh
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
current_branch=$(git rev-parse --abbrev-ref HEAD)
|
|
4
|
+
|
|
5
|
+
if [ "$current_branch" != "main" ]; then
|
|
6
|
+
echo "Error: Current branch not 'main'. Release impossible you are on '$current_branch'."
|
|
7
|
+
exit 1
|
|
8
|
+
fi
|
|
9
|
+
|
|
10
|
+
echo "Current branch — 'main'."
|
|
11
|
+
exit 0
|
package/commitizen.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { execSync } = require("child_process");
|
|
2
|
+
|
|
3
|
+
function getGitBranch() {
|
|
4
|
+
const res = execSync("git rev-parse --abbrev-ref HEAD", { encoding: "utf8" });
|
|
5
|
+
return res.trim();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
module.exports = {
|
|
9
|
+
types: [
|
|
10
|
+
{
|
|
11
|
+
value: "fix",
|
|
12
|
+
name: "fix: Bug fixes",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
value: "feat",
|
|
16
|
+
name: "feat: Adding new functionality",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
value: "refactor",
|
|
20
|
+
name: "refactor: Code edits without fixing bugs or adding new features",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
value: "build",
|
|
24
|
+
name: "build: Building a project or changing external dependencies",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
value: "docs",
|
|
28
|
+
name: "docs: Documentation Update",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
value: "docs",
|
|
32
|
+
name: "docs: Edits to the project documentation or individual parts of the project",
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
value: "test",
|
|
36
|
+
name: "test: Adding tests",
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
scopes: [
|
|
40
|
+
{
|
|
41
|
+
name: getGitBranch(),
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
|
|
45
|
+
allowCustomScopes: true,
|
|
46
|
+
|
|
47
|
+
allowBreakingChanges: false,
|
|
48
|
+
|
|
49
|
+
subjectLimit: 255,
|
|
50
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var silentium = require('silentium');
|
|
4
|
+
var silentiumComponents = require('silentium-components');
|
|
5
|
+
|
|
6
|
+
function Validated(errors) {
|
|
7
|
+
return !Object.values(errors).some(
|
|
8
|
+
(errorValues) => errorValues.length > 0
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function ValidationErrors(form) {
|
|
13
|
+
const $form = silentium.ActualMessage(form);
|
|
14
|
+
return silentium.Message((resolve, reject) => {
|
|
15
|
+
const formDc = silentium.DestroyContainer();
|
|
16
|
+
$form.then((form2) => {
|
|
17
|
+
formDc.destroy();
|
|
18
|
+
const entries = form2.map((i) => {
|
|
19
|
+
return silentium.All(
|
|
20
|
+
i.key,
|
|
21
|
+
silentium.Applied(
|
|
22
|
+
silentium.All(
|
|
23
|
+
...i.rules.map((rule) => {
|
|
24
|
+
return formDc.add(rule(i.value));
|
|
25
|
+
})
|
|
26
|
+
),
|
|
27
|
+
(items) => items.filter(ExcludeTrue).map(ErrorFormat)
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
silentium.Applied(silentium.All(...entries), (e) => Object.fromEntries(e)).catch(reject).then(resolve);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function ErrorFormat(v) {
|
|
36
|
+
return v === false ? "Error!" : v;
|
|
37
|
+
}
|
|
38
|
+
function ExcludeTrue(v) {
|
|
39
|
+
return v !== true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function ValidationErrorsHappened(base) {
|
|
43
|
+
return Object.fromEntries(
|
|
44
|
+
Object.entries(base).filter((entry) => entry[1].length > 0)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function ValidationErrorsSummary(errors) {
|
|
49
|
+
return Object.values(errors).flat();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function ValidationErrorsTouched($form, $errors) {
|
|
53
|
+
const dirtyForm = silentiumComponents.Dirty($form);
|
|
54
|
+
silentium.Chainable(dirtyForm).chain($form);
|
|
55
|
+
const touchedForm = silentiumComponents.MergeAccumulation(dirtyForm);
|
|
56
|
+
const errorsTouched = silentium.All(silentium.Applied(touchedForm, Object.keys), $errors);
|
|
57
|
+
return silentium.Applied(errorsTouched, ([touched, errors]) => {
|
|
58
|
+
return Object.fromEntries(
|
|
59
|
+
Object.entries(errors).filter((entry) => touched.includes(entry[0]))
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function ValidationItems(form, rules) {
|
|
65
|
+
return Object.keys(form).map((key) => {
|
|
66
|
+
return {
|
|
67
|
+
key,
|
|
68
|
+
value: form[key],
|
|
69
|
+
rules: rules[key]
|
|
70
|
+
};
|
|
71
|
+
}).filter((item) => !!item.rules);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const Required = (v) => !!v || "Field required";
|
|
75
|
+
const Integer = (v) => Number.isInteger(v) || "Must be integer";
|
|
76
|
+
|
|
77
|
+
exports.Integer = Integer;
|
|
78
|
+
exports.Required = Required;
|
|
79
|
+
exports.Validated = Validated;
|
|
80
|
+
exports.ValidationErrors = ValidationErrors;
|
|
81
|
+
exports.ValidationErrorsHappened = ValidationErrorsHappened;
|
|
82
|
+
exports.ValidationErrorsSummary = ValidationErrorsSummary;
|
|
83
|
+
exports.ValidationErrorsTouched = ValidationErrorsTouched;
|
|
84
|
+
exports.ValidationItems = ValidationItems;
|
|
85
|
+
//# sourceMappingURL=silentium_validation.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"silentium_validation.cjs","sources":["../src/models/Validated.ts","../src/models/ValidationErrors.ts","../src/models/ValidationErrorsHappened.ts","../src/models/ValidationErrorsSummary.ts","../src/models/ValidationErrorsTouched.ts","../src/models/ValidationItems.ts","../src/rules.ts"],"sourcesContent":["import { ValidationErrorType } from \"@/types\";\n\n/**\n * Check if there are any errors in the errors object\n * Returns a boolean type\n */\nexport function Validated(errors: ValidationErrorType) {\n return !Object.values(errors).some(\n (errorValues: any) => errorValues.length > 0,\n );\n}\n","import {\n ActualMessage,\n All,\n Applied,\n DestroyContainer,\n MaybeMessage,\n Message,\n MessageType,\n} from \"silentium\";\nimport { ValidationErrorType, ValidationItem } from \"@/types\";\n\n/**\n * Accepts a set of items that need to be validated\n * and when rules produce values, returns the overall set\n * of errors for the given configuration\n */\nexport function ValidationErrors(\n form: MaybeMessage<ValidationItem[]>,\n): MessageType<ValidationErrorType> {\n const $form = ActualMessage(form);\n return Message((resolve, reject) => {\n const formDc = DestroyContainer();\n $form.then((form) => {\n formDc.destroy();\n const entries = form.map((i) => {\n return All(\n i.key,\n Applied(\n All(\n ...i.rules.map((rule) => {\n return formDc.add(rule(i.value));\n }),\n ),\n (items) => items.filter(ExcludeTrue).map(ErrorFormat),\n ) as MessageType<string[]>,\n );\n });\n Applied(All(...entries), (e: any) => Object.fromEntries(e))\n .catch(reject)\n .then(resolve);\n });\n });\n}\n\nfunction ErrorFormat(v: boolean | string) {\n return v === false ? \"Error!\" : v;\n}\n\nfunction ExcludeTrue(v: boolean | string) {\n return v !== true;\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Show only the errors that exist, fields without errors are not shown\n */\nexport function ValidationErrorsHappened(base: ValidationErrorType) {\n return Object.fromEntries(\n Object.entries(base).filter((entry) => entry[1].length > 0),\n );\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Overall array of all errors\n */\nexport function ValidationErrorsSummary(errors: ValidationErrorType) {\n return Object.values(errors).flat();\n}\n","import { All, Applied, Chainable, MessageType } from \"silentium\";\nimport { ValidationErrorType } from \"@/types\";\nimport { Dirty, MergeAccumulation } from \"silentium-components\";\n\n/**\n * Validation errors are only those that correspond to changed form fields\n */\nexport function ValidationErrorsTouched(\n $form: MessageType<Record<string, unknown>>,\n $errors: MessageType<ValidationErrorType>,\n) {\n const dirtyForm = Dirty($form);\n Chainable(dirtyForm).chain($form);\n const touchedForm = MergeAccumulation(dirtyForm);\n const errorsTouched = All(Applied(touchedForm, Object.keys), $errors);\n return Applied(errorsTouched, ([touched, errors]) => {\n return Object.fromEntries(\n Object.entries(errors).filter((entry) => touched.includes(entry[0])),\n );\n });\n}\n","import { ConstructorType } from \"silentium\";\nimport { ValidationRule } from \"@/types\";\n\nexport type FormType = Record<string, unknown>;\nexport type FormRulesType = Record<\n string,\n ConstructorType<any, ValidationRule>[]\n>;\n\n/**\n * Get a set of all validation rules\n * for each form field\n */\nexport function ValidationItems(form: FormType, rules: FormRulesType) {\n return Object.keys(form)\n .map((key) => {\n return {\n key,\n value: form[key],\n rules: rules[key],\n };\n })\n .filter((item) => !!item.rules);\n}\n","/**\n * Validation rule, what requires truthy value\n */\nexport const Required = (v: unknown) => !!v || \"Field required\";\n\n/**\n * Validation rule what requires integer value\n */\nexport const Integer = (v: unknown) => Number.isInteger(v) || \"Must be integer\";\n"],"names":["ActualMessage","Message","DestroyContainer","form","All","Applied","Dirty","Chainable","MergeAccumulation"],"mappings":";;;;;AAMO,SAAS,UAAU,MAAA,EAA6B;AACrD,EAAA,OAAO,CAAC,MAAA,CAAO,MAAA,CAAO,MAAM,CAAA,CAAE,IAAA;AAAA,IAC5B,CAAC,WAAA,KAAqB,WAAA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O,MAAM,QAAA,GAAW,CAAC,CAAA,KAAe,CAAC,CAAC,CAAA,IAAK;AAKxC,MAAM,UAAU,CAAC,CAAA,KAAe,MAAA,CAAO,SAAA,CAAU,CAAC,CAAA,IAAK;;;;;;;;;;;"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as silentium from 'silentium';
|
|
2
|
+
import { ConstructorType, MaybeMessage, MessageType } from 'silentium';
|
|
3
|
+
|
|
4
|
+
type ValidationErrorType = Record<string, string[]>;
|
|
5
|
+
type ValidationRule = MaybeMessage<string | boolean>;
|
|
6
|
+
interface ValidationItem {
|
|
7
|
+
value: unknown;
|
|
8
|
+
key: string;
|
|
9
|
+
rules: ConstructorType<any, ValidationRule>[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Check if there are any errors in the errors object
|
|
14
|
+
* Returns a boolean type
|
|
15
|
+
*/
|
|
16
|
+
declare function Validated(errors: ValidationErrorType): boolean;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Accepts a set of items that need to be validated
|
|
20
|
+
* and when rules produce values, returns the overall set
|
|
21
|
+
* of errors for the given configuration
|
|
22
|
+
*/
|
|
23
|
+
declare function ValidationErrors(form: MaybeMessage<ValidationItem[]>): MessageType<ValidationErrorType>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Show only the errors that exist, fields without errors are not shown
|
|
27
|
+
*/
|
|
28
|
+
declare function ValidationErrorsHappened(base: ValidationErrorType): {
|
|
29
|
+
[k: string]: string[];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Overall array of all errors
|
|
34
|
+
*/
|
|
35
|
+
declare function ValidationErrorsSummary(errors: ValidationErrorType): string[];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Validation errors are only those that correspond to changed form fields
|
|
39
|
+
*/
|
|
40
|
+
declare function ValidationErrorsTouched($form: MessageType<Record<string, unknown>>, $errors: MessageType<ValidationErrorType>): silentium.MessageRx<{
|
|
41
|
+
[k: string]: string[];
|
|
42
|
+
}>;
|
|
43
|
+
|
|
44
|
+
type FormType = Record<string, unknown>;
|
|
45
|
+
type FormRulesType = Record<string, ConstructorType<any, ValidationRule>[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Get a set of all validation rules
|
|
48
|
+
* for each form field
|
|
49
|
+
*/
|
|
50
|
+
declare function ValidationItems(form: FormType, rules: FormRulesType): {
|
|
51
|
+
key: string;
|
|
52
|
+
value: unknown;
|
|
53
|
+
rules: ConstructorType<any, ValidationRule>[];
|
|
54
|
+
}[];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Validation rule, what requires truthy value
|
|
58
|
+
*/
|
|
59
|
+
declare const Required: (v: unknown) => true | "Field required";
|
|
60
|
+
/**
|
|
61
|
+
* Validation rule what requires integer value
|
|
62
|
+
*/
|
|
63
|
+
declare const Integer: (v: unknown) => true | "Must be integer";
|
|
64
|
+
|
|
65
|
+
export { Integer, Required, Validated, ValidationErrors, ValidationErrorsHappened, ValidationErrorsSummary, ValidationErrorsTouched, ValidationItems };
|
|
66
|
+
export type { FormRulesType, FormType, ValidationErrorType, ValidationItem, ValidationRule };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ActualMessage, Message, DestroyContainer, All, Applied, Chainable } from 'silentium';
|
|
2
|
+
import { Dirty, MergeAccumulation } from 'silentium-components';
|
|
3
|
+
|
|
4
|
+
function Validated(errors) {
|
|
5
|
+
return !Object.values(errors).some(
|
|
6
|
+
(errorValues) => errorValues.length > 0
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function ValidationErrors(form) {
|
|
11
|
+
const $form = ActualMessage(form);
|
|
12
|
+
return Message((resolve, reject) => {
|
|
13
|
+
const formDc = DestroyContainer();
|
|
14
|
+
$form.then((form2) => {
|
|
15
|
+
formDc.destroy();
|
|
16
|
+
const entries = form2.map((i) => {
|
|
17
|
+
return All(
|
|
18
|
+
i.key,
|
|
19
|
+
Applied(
|
|
20
|
+
All(
|
|
21
|
+
...i.rules.map((rule) => {
|
|
22
|
+
return formDc.add(rule(i.value));
|
|
23
|
+
})
|
|
24
|
+
),
|
|
25
|
+
(items) => items.filter(ExcludeTrue).map(ErrorFormat)
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
Applied(All(...entries), (e) => Object.fromEntries(e)).catch(reject).then(resolve);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function ErrorFormat(v) {
|
|
34
|
+
return v === false ? "Error!" : v;
|
|
35
|
+
}
|
|
36
|
+
function ExcludeTrue(v) {
|
|
37
|
+
return v !== true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function ValidationErrorsHappened(base) {
|
|
41
|
+
return Object.fromEntries(
|
|
42
|
+
Object.entries(base).filter((entry) => entry[1].length > 0)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function ValidationErrorsSummary(errors) {
|
|
47
|
+
return Object.values(errors).flat();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function ValidationErrorsTouched($form, $errors) {
|
|
51
|
+
const dirtyForm = Dirty($form);
|
|
52
|
+
Chainable(dirtyForm).chain($form);
|
|
53
|
+
const touchedForm = MergeAccumulation(dirtyForm);
|
|
54
|
+
const errorsTouched = All(Applied(touchedForm, Object.keys), $errors);
|
|
55
|
+
return Applied(errorsTouched, ([touched, errors]) => {
|
|
56
|
+
return Object.fromEntries(
|
|
57
|
+
Object.entries(errors).filter((entry) => touched.includes(entry[0]))
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function ValidationItems(form, rules) {
|
|
63
|
+
return Object.keys(form).map((key) => {
|
|
64
|
+
return {
|
|
65
|
+
key,
|
|
66
|
+
value: form[key],
|
|
67
|
+
rules: rules[key]
|
|
68
|
+
};
|
|
69
|
+
}).filter((item) => !!item.rules);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const Required = (v) => !!v || "Field required";
|
|
73
|
+
const Integer = (v) => Number.isInteger(v) || "Must be integer";
|
|
74
|
+
|
|
75
|
+
export { Integer, Required, Validated, ValidationErrors, ValidationErrorsHappened, ValidationErrorsSummary, ValidationErrorsTouched, ValidationItems };
|
|
76
|
+
//# sourceMappingURL=silentium_validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"silentium_validation.js","sources":["../src/models/Validated.ts","../src/models/ValidationErrors.ts","../src/models/ValidationErrorsHappened.ts","../src/models/ValidationErrorsSummary.ts","../src/models/ValidationErrorsTouched.ts","../src/models/ValidationItems.ts","../src/rules.ts"],"sourcesContent":["import { ValidationErrorType } from \"@/types\";\n\n/**\n * Check if there are any errors in the errors object\n * Returns a boolean type\n */\nexport function Validated(errors: ValidationErrorType) {\n return !Object.values(errors).some(\n (errorValues: any) => errorValues.length > 0,\n );\n}\n","import {\n ActualMessage,\n All,\n Applied,\n DestroyContainer,\n MaybeMessage,\n Message,\n MessageType,\n} from \"silentium\";\nimport { ValidationErrorType, ValidationItem } from \"@/types\";\n\n/**\n * Accepts a set of items that need to be validated\n * and when rules produce values, returns the overall set\n * of errors for the given configuration\n */\nexport function ValidationErrors(\n form: MaybeMessage<ValidationItem[]>,\n): MessageType<ValidationErrorType> {\n const $form = ActualMessage(form);\n return Message((resolve, reject) => {\n const formDc = DestroyContainer();\n $form.then((form) => {\n formDc.destroy();\n const entries = form.map((i) => {\n return All(\n i.key,\n Applied(\n All(\n ...i.rules.map((rule) => {\n return formDc.add(rule(i.value));\n }),\n ),\n (items) => items.filter(ExcludeTrue).map(ErrorFormat),\n ) as MessageType<string[]>,\n );\n });\n Applied(All(...entries), (e: any) => Object.fromEntries(e))\n .catch(reject)\n .then(resolve);\n });\n });\n}\n\nfunction ErrorFormat(v: boolean | string) {\n return v === false ? \"Error!\" : v;\n}\n\nfunction ExcludeTrue(v: boolean | string) {\n return v !== true;\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Show only the errors that exist, fields without errors are not shown\n */\nexport function ValidationErrorsHappened(base: ValidationErrorType) {\n return Object.fromEntries(\n Object.entries(base).filter((entry) => entry[1].length > 0),\n );\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Overall array of all errors\n */\nexport function ValidationErrorsSummary(errors: ValidationErrorType) {\n return Object.values(errors).flat();\n}\n","import { All, Applied, Chainable, MessageType } from \"silentium\";\nimport { ValidationErrorType } from \"@/types\";\nimport { Dirty, MergeAccumulation } from \"silentium-components\";\n\n/**\n * Validation errors are only those that correspond to changed form fields\n */\nexport function ValidationErrorsTouched(\n $form: MessageType<Record<string, unknown>>,\n $errors: MessageType<ValidationErrorType>,\n) {\n const dirtyForm = Dirty($form);\n Chainable(dirtyForm).chain($form);\n const touchedForm = MergeAccumulation(dirtyForm);\n const errorsTouched = All(Applied(touchedForm, Object.keys), $errors);\n return Applied(errorsTouched, ([touched, errors]) => {\n return Object.fromEntries(\n Object.entries(errors).filter((entry) => touched.includes(entry[0])),\n );\n });\n}\n","import { ConstructorType } from \"silentium\";\nimport { ValidationRule } from \"@/types\";\n\nexport type FormType = Record<string, unknown>;\nexport type FormRulesType = Record<\n string,\n ConstructorType<any, ValidationRule>[]\n>;\n\n/**\n * Get a set of all validation rules\n * for each form field\n */\nexport function ValidationItems(form: FormType, rules: FormRulesType) {\n return Object.keys(form)\n .map((key) => {\n return {\n key,\n value: form[key],\n rules: rules[key],\n };\n })\n .filter((item) => !!item.rules);\n}\n","/**\n * Validation rule, what requires truthy value\n */\nexport const Required = (v: unknown) => !!v || \"Field required\";\n\n/**\n * Validation rule what requires integer value\n */\nexport const Integer = (v: unknown) => Number.isInteger(v) || \"Must be integer\";\n"],"names":["form"],"mappings":";;;AAMO,SAAS,UAAU,MAAA,EAA6B;AACrD,EAAA,OAAO,CAAC,MAAA,CAAO,MAAA,CAAO,MAAM,CAAA,CAAE,IAAA;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A;AAClC;;ACpBO,MAAM,QAAA,GAAW,CAAC,CAAA,KAAe,CAAC,CAAC,CAAA,IAAK;AAKxC,MAAM,UAAU,CAAC,CAAA,KAAe,MAAA,CAAO,SAAA,CAAU,CAAC,CAAA,IAAK;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var silentium_validation=function(e,t,r){"use strict";function n(e){return!1===e?"Error!":e}function i(e){return!0!==e}return e.Integer=e=>Number.isInteger(e)||"Must be integer",e.Required=e=>!!e||"Field required",e.Validated=function(e){return!Object.values(e).some(e=>e.length>0)},e.ValidationErrors=function(e){const r=t.ActualMessage(e);return t.Message((e,l)=>{const u=t.DestroyContainer();r.then(r=>{u.destroy();const s=r.map(e=>t.All(e.key,t.Applied(t.All(...e.rules.map(t=>u.add(t(e.value)))),e=>e.filter(i).map(n))));t.Applied(t.All(...s),e=>Object.fromEntries(e)).catch(l).then(e)})})},e.ValidationErrorsHappened=function(e){return Object.fromEntries(Object.entries(e).filter(e=>e[1].length>0))},e.ValidationErrorsSummary=function(e){return Object.values(e).flat()},e.ValidationErrorsTouched=function(e,n){const i=r.Dirty(e);t.Chainable(i).chain(e);const l=r.MergeAccumulation(i),u=t.All(t.Applied(l,Object.keys),n);return t.Applied(u,([e,t])=>Object.fromEntries(Object.entries(t).filter(t=>e.includes(t[0]))))},e.ValidationItems=function(e,t){return Object.keys(e).map(r=>({key:r,value:e[r],rules:t[r]})).filter(e=>!!e.rules)},e}({},silentium,silentiumComponents);
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{ActualMessage as e,Message as t,DestroyContainer as n,All as r,Applied as i,Chainable as u}from"silentium";import{Dirty as c,MergeAccumulation as o}from"silentium-components";function s(e){return!Object.values(e).some(e=>e.length>0)}function f(u){const c=e(u);return t((e,t)=>{const u=n();c.then(n=>{u.destroy();const c=n.map(e=>r(e.key,i(r(...e.rules.map(t=>u.add(t(e.value)))),e=>e.filter(m).map(l))));i(r(...c),e=>Object.fromEntries(e)).catch(t).then(e)})})}function l(e){return!1===e?"Error!":e}function m(e){return!0!==e}function a(e){return Object.fromEntries(Object.entries(e).filter(e=>e[1].length>0))}function b(e){return Object.values(e).flat()}function j(e,t){const n=c(e);u(n).chain(e);const s=o(n),f=r(i(s,Object.keys),t);return i(f,([e,t])=>Object.fromEntries(Object.entries(t).filter(t=>e.includes(t[0]))))}function O(e,t){return Object.keys(e).map(n=>({key:n,value:e[n],rules:t[n]})).filter(e=>!!e.rules)}const p=e=>!!e||"Field required",d=e=>Number.isInteger(e)||"Must be integer";export{d as Integer,p as Required,s as Validated,f as ValidationErrors,a as ValidationErrorsHappened,b as ValidationErrorsSummary,j as ValidationErrorsTouched,O as ValidationItems};
|
|
2
|
+
//# sourceMappingURL=silentium_validation.min.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"silentium_validation.min.mjs","sources":["../src/models/Validated.ts","../src/models/ValidationErrors.ts","../src/models/ValidationErrorsHappened.ts","../src/models/ValidationErrorsSummary.ts","../src/models/ValidationErrorsTouched.ts","../src/models/ValidationItems.ts","../src/rules.ts"],"sourcesContent":["import { ValidationErrorType } from \"@/types\";\n\n/**\n * Check if there are any errors in the errors object\n * Returns a boolean type\n */\nexport function Validated(errors: ValidationErrorType) {\n return !Object.values(errors).some(\n (errorValues: any) => errorValues.length > 0,\n );\n}\n","import {\n ActualMessage,\n All,\n Applied,\n DestroyContainer,\n MaybeMessage,\n Message,\n MessageType,\n} from \"silentium\";\nimport { ValidationErrorType, ValidationItem } from \"@/types\";\n\n/**\n * Accepts a set of items that need to be validated\n * and when rules produce values, returns the overall set\n * of errors for the given configuration\n */\nexport function ValidationErrors(\n form: MaybeMessage<ValidationItem[]>,\n): MessageType<ValidationErrorType> {\n const $form = ActualMessage(form);\n return Message((resolve, reject) => {\n const formDc = DestroyContainer();\n $form.then((form) => {\n formDc.destroy();\n const entries = form.map((i) => {\n return All(\n i.key,\n Applied(\n All(\n ...i.rules.map((rule) => {\n return formDc.add(rule(i.value));\n }),\n ),\n (items) => items.filter(ExcludeTrue).map(ErrorFormat),\n ) as MessageType<string[]>,\n );\n });\n Applied(All(...entries), (e: any) => Object.fromEntries(e))\n .catch(reject)\n .then(resolve);\n });\n });\n}\n\nfunction ErrorFormat(v: boolean | string) {\n return v === false ? \"Error!\" : v;\n}\n\nfunction ExcludeTrue(v: boolean | string) {\n return v !== true;\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Show only the errors that exist, fields without errors are not shown\n */\nexport function ValidationErrorsHappened(base: ValidationErrorType) {\n return Object.fromEntries(\n Object.entries(base).filter((entry) => entry[1].length > 0),\n );\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Overall array of all errors\n */\nexport function ValidationErrorsSummary(errors: ValidationErrorType) {\n return Object.values(errors).flat();\n}\n","import { All, Applied, Chainable, MessageType } from \"silentium\";\nimport { ValidationErrorType } from \"@/types\";\nimport { Dirty, MergeAccumulation } from \"silentium-components\";\n\n/**\n * Validation errors are only those that correspond to changed form fields\n */\nexport function ValidationErrorsTouched(\n $form: MessageType<Record<string, unknown>>,\n $errors: MessageType<ValidationErrorType>,\n) {\n const dirtyForm = Dirty($form);\n Chainable(dirtyForm).chain($form);\n const touchedForm = MergeAccumulation(dirtyForm);\n const errorsTouched = All(Applied(touchedForm, Object.keys), $errors);\n return Applied(errorsTouched, ([touched, errors]) => {\n return Object.fromEntries(\n Object.entries(errors).filter((entry) => touched.includes(entry[0])),\n );\n });\n}\n","import { ConstructorType } from \"silentium\";\nimport { ValidationRule } from \"@/types\";\n\nexport type FormType = Record<string, unknown>;\nexport type FormRulesType = Record<\n string,\n ConstructorType<any, ValidationRule>[]\n>;\n\n/**\n * Get a set of all validation rules\n * for each form field\n */\nexport function ValidationItems(form: FormType, rules: FormRulesType) {\n return Object.keys(form)\n .map((key) => {\n return {\n key,\n value: form[key],\n rules: rules[key],\n };\n })\n .filter((item) => !!item.rules);\n}\n","/**\n * Validation rule, what requires truthy value\n */\nexport const Required = (v: unknown) => !!v || \"Field required\";\n\n/**\n * Validation rule what requires integer value\n */\nexport const Integer = (v: unknown) => Number.isInteger(v) || \"Must be integer\";\n"],"names":["Validated","errors","Object","values","some","errorValues","length","ValidationErrors","form","$form","ActualMessage","Message","resolve","reject","formDc","DestroyContainer","then","destroy","entries","map","i","All","key","Applied","rules","rule","add","value","items","filter","ExcludeTrue","ErrorFormat","e","fromEntries","catch","v","ValidationErrorsHappened","base","entry","ValidationErrorsSummary","flat","ValidationErrorsTouched","$errors","dirtyForm","Dirty","Chainable","chain","touchedForm","MergeAccumulation","errorsTouched","keys","touched","includes","ValidationItems","item","Required","Integer","Number","isInteger"],"mappings":"sLAMO,SAASA,EAAUC,GACxB,OAAQC,OAAOC,OAAOF,GAAQG,KAC3BC,GAAqBA,EAAYC,OAAS,EAE/C,CCMO,SAASC,EACdC,GAEA,MAAMC,EAAQC,EAAcF,GAC5B,OAAOG,EAAQ,CAACC,EAASC,KACvB,MAAMC,EAASC,IACfN,EAAMO,KAAMR,IACVM,EAAOG,UACP,MAAMC,EAAUV,EAAKW,IAAKC,GACjBC,EACLD,EAAEE,IACFC,EACEF,KACKD,EAAEI,MAAML,IAAKM,GACPX,EAAOY,IAAID,EAAKL,EAAEO,UAG5BC,GAAUA,EAAMC,OAAOC,GAAaX,IAAIY,MAI/CR,EAAQF,KAAOH,GAAWc,GAAW9B,OAAO+B,YAAYD,IACrDE,MAAMrB,GACNG,KAAKJ,MAGd,CAEA,SAASmB,EAAYI,GACnB,OAAa,IAANA,EAAc,SAAWA,CAClC,CAEA,SAASL,EAAYK,GACnB,OAAa,IAANA,CACT,CC7CO,SAASC,EAAyBC,GACvC,OAAOnC,OAAO+B,YACZ/B,OAAOgB,QAAQmB,GAAMR,OAAQS,GAAUA,EAAM,GAAGhC,OAAS,GAE7D,CCJO,SAASiC,EAAwBtC,GACtC,OAAOC,OAAOC,OAAOF,GAAQuC,MAC/B,CCAO,SAASC,EACdhC,EACAiC,GAEA,MAAMC,EAAYC,EAAMnC,GACxBoC,EAAUF,GAAWG,MAAMrC,GAC3B,MAAMsC,EAAcC,EAAkBL,GAChCM,EAAgB5B,EAAIE,EAAQwB,EAAa7C,OAAOgD,MAAOR,GAC7D,OAAOnB,EAAQ0B,EAAe,EAAEE,EAASlD,KAChCC,OAAO+B,YACZ/B,OAAOgB,QAAQjB,GAAQ4B,OAAQS,GAAUa,EAAQC,SAASd,EAAM,MAGtE,CCPO,SAASe,EAAgB7C,EAAgBgB,GAC9C,OAAOtB,OAAOgD,KAAK1C,GAChBW,IAAKG,IACG,CACLA,MACAK,MAAOnB,EAAKc,GACZE,MAAOA,EAAMF,MAGhBO,OAAQyB,KAAWA,EAAK9B,MAC7B,CCpBO,MAAM+B,EAAYpB,KAAiBA,GAAK,iBAKlCqB,EAAWrB,GAAesB,OAAOC,UAAUvB,IAAM"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ActualMessage, Message, DestroyContainer, All, Applied, Chainable } from 'silentium';
|
|
2
|
+
import { Dirty, MergeAccumulation } from 'silentium-components';
|
|
3
|
+
|
|
4
|
+
function Validated(errors) {
|
|
5
|
+
return !Object.values(errors).some(
|
|
6
|
+
(errorValues) => errorValues.length > 0
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function ValidationErrors(form) {
|
|
11
|
+
const $form = ActualMessage(form);
|
|
12
|
+
return Message((resolve, reject) => {
|
|
13
|
+
const formDc = DestroyContainer();
|
|
14
|
+
$form.then((form2) => {
|
|
15
|
+
formDc.destroy();
|
|
16
|
+
const entries = form2.map((i) => {
|
|
17
|
+
return All(
|
|
18
|
+
i.key,
|
|
19
|
+
Applied(
|
|
20
|
+
All(
|
|
21
|
+
...i.rules.map((rule) => {
|
|
22
|
+
return formDc.add(rule(i.value));
|
|
23
|
+
})
|
|
24
|
+
),
|
|
25
|
+
(items) => items.filter(ExcludeTrue).map(ErrorFormat)
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
Applied(All(...entries), (e) => Object.fromEntries(e)).catch(reject).then(resolve);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function ErrorFormat(v) {
|
|
34
|
+
return v === false ? "Error!" : v;
|
|
35
|
+
}
|
|
36
|
+
function ExcludeTrue(v) {
|
|
37
|
+
return v !== true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function ValidationErrorsHappened(base) {
|
|
41
|
+
return Object.fromEntries(
|
|
42
|
+
Object.entries(base).filter((entry) => entry[1].length > 0)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function ValidationErrorsSummary(errors) {
|
|
47
|
+
return Object.values(errors).flat();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function ValidationErrorsTouched($form, $errors) {
|
|
51
|
+
const dirtyForm = Dirty($form);
|
|
52
|
+
Chainable(dirtyForm).chain($form);
|
|
53
|
+
const touchedForm = MergeAccumulation(dirtyForm);
|
|
54
|
+
const errorsTouched = All(Applied(touchedForm, Object.keys), $errors);
|
|
55
|
+
return Applied(errorsTouched, ([touched, errors]) => {
|
|
56
|
+
return Object.fromEntries(
|
|
57
|
+
Object.entries(errors).filter((entry) => touched.includes(entry[0]))
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function ValidationItems(form, rules) {
|
|
63
|
+
return Object.keys(form).map((key) => {
|
|
64
|
+
return {
|
|
65
|
+
key,
|
|
66
|
+
value: form[key],
|
|
67
|
+
rules: rules[key]
|
|
68
|
+
};
|
|
69
|
+
}).filter((item) => !!item.rules);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const Required = (v) => !!v || "Field required";
|
|
73
|
+
const Integer = (v) => Number.isInteger(v) || "Must be integer";
|
|
74
|
+
|
|
75
|
+
export { Integer, Required, Validated, ValidationErrors, ValidationErrorsHappened, ValidationErrorsSummary, ValidationErrorsTouched, ValidationItems };
|
|
76
|
+
//# sourceMappingURL=silentium_validation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"silentium_validation.mjs","sources":["../src/models/Validated.ts","../src/models/ValidationErrors.ts","../src/models/ValidationErrorsHappened.ts","../src/models/ValidationErrorsSummary.ts","../src/models/ValidationErrorsTouched.ts","../src/models/ValidationItems.ts","../src/rules.ts"],"sourcesContent":["import { ValidationErrorType } from \"@/types\";\n\n/**\n * Check if there are any errors in the errors object\n * Returns a boolean type\n */\nexport function Validated(errors: ValidationErrorType) {\n return !Object.values(errors).some(\n (errorValues: any) => errorValues.length > 0,\n );\n}\n","import {\n ActualMessage,\n All,\n Applied,\n DestroyContainer,\n MaybeMessage,\n Message,\n MessageType,\n} from \"silentium\";\nimport { ValidationErrorType, ValidationItem } from \"@/types\";\n\n/**\n * Accepts a set of items that need to be validated\n * and when rules produce values, returns the overall set\n * of errors for the given configuration\n */\nexport function ValidationErrors(\n form: MaybeMessage<ValidationItem[]>,\n): MessageType<ValidationErrorType> {\n const $form = ActualMessage(form);\n return Message((resolve, reject) => {\n const formDc = DestroyContainer();\n $form.then((form) => {\n formDc.destroy();\n const entries = form.map((i) => {\n return All(\n i.key,\n Applied(\n All(\n ...i.rules.map((rule) => {\n return formDc.add(rule(i.value));\n }),\n ),\n (items) => items.filter(ExcludeTrue).map(ErrorFormat),\n ) as MessageType<string[]>,\n );\n });\n Applied(All(...entries), (e: any) => Object.fromEntries(e))\n .catch(reject)\n .then(resolve);\n });\n });\n}\n\nfunction ErrorFormat(v: boolean | string) {\n return v === false ? \"Error!\" : v;\n}\n\nfunction ExcludeTrue(v: boolean | string) {\n return v !== true;\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Show only the errors that exist, fields without errors are not shown\n */\nexport function ValidationErrorsHappened(base: ValidationErrorType) {\n return Object.fromEntries(\n Object.entries(base).filter((entry) => entry[1].length > 0),\n );\n}\n","import { ValidationErrorType } from \"@/types\";\n\n/**\n * Overall array of all errors\n */\nexport function ValidationErrorsSummary(errors: ValidationErrorType) {\n return Object.values(errors).flat();\n}\n","import { All, Applied, Chainable, MessageType } from \"silentium\";\nimport { ValidationErrorType } from \"@/types\";\nimport { Dirty, MergeAccumulation } from \"silentium-components\";\n\n/**\n * Validation errors are only those that correspond to changed form fields\n */\nexport function ValidationErrorsTouched(\n $form: MessageType<Record<string, unknown>>,\n $errors: MessageType<ValidationErrorType>,\n) {\n const dirtyForm = Dirty($form);\n Chainable(dirtyForm).chain($form);\n const touchedForm = MergeAccumulation(dirtyForm);\n const errorsTouched = All(Applied(touchedForm, Object.keys), $errors);\n return Applied(errorsTouched, ([touched, errors]) => {\n return Object.fromEntries(\n Object.entries(errors).filter((entry) => touched.includes(entry[0])),\n );\n });\n}\n","import { ConstructorType } from \"silentium\";\nimport { ValidationRule } from \"@/types\";\n\nexport type FormType = Record<string, unknown>;\nexport type FormRulesType = Record<\n string,\n ConstructorType<any, ValidationRule>[]\n>;\n\n/**\n * Get a set of all validation rules\n * for each form field\n */\nexport function ValidationItems(form: FormType, rules: FormRulesType) {\n return Object.keys(form)\n .map((key) => {\n return {\n key,\n value: form[key],\n rules: rules[key],\n };\n })\n .filter((item) => !!item.rules);\n}\n","/**\n * Validation rule, what requires truthy value\n */\nexport const Required = (v: unknown) => !!v || \"Field required\";\n\n/**\n * Validation rule what requires integer value\n */\nexport const Integer = (v: unknown) => Number.isInteger(v) || \"Must be integer\";\n"],"names":["form"],"mappings":";;;AAMO,SAAS,UAAU,MAAA,EAA6B;AACrD,EAAA,OAAO,CAAC,MAAA,CAAO,MAAA,CAAO,MAAM,CAAA,CAAE,IAAA;AAAA,IAC5B,CAAC,WAAA,KAAqB,WAAA,CAAY,MAAA,GAAS;AAAA,GAC7C;AACF;;ACMO,SAAS,iBACd,IAAA,EACkC;AAClC,EAAA,MAAM,KAAA,GAAQ,cAAc,IAAI,CAAA;AAChC,EAAA,OAAO,OAAA,CAAQ,CAAC,OAAA,EAAS,MAAA,KAAW;A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