@splitty-test/validation 0.1.1 → 0.2.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.
@@ -1,106 +0,0 @@
1
- import { reactive, ref, type Ref } from 'vue';
2
- import { forIn } from 'lodash-es';
3
- import { FieldValidator, type FieldValidatorConfig } from './FieldValidatorClass';
4
- import { asyncForEach } from 'modern-async';
5
-
6
- export interface ValidatorConfig {
7
- fields?: Record<string, FieldValidatorConfig>;
8
- }
9
-
10
- export interface ValidationGroup {
11
- dirty: boolean;
12
- fields: Record<string, FieldValidator>;
13
- touched: boolean;
14
- validated: boolean;
15
- }
16
-
17
- export class Validator {
18
- dirty: boolean;
19
- errors: string[];
20
- fields: Record<string, FieldValidator>;
21
- groups: Record<string, ValidationGroup>;
22
- touched: boolean;
23
- validated: boolean;
24
-
25
- constructor(config: ValidatorConfig = {}) {
26
- this.dirty = false;
27
- this.errors = [];
28
- this.fields = {};
29
- this.groups = {};
30
- this.touched = false;
31
- this.validated = false;
32
-
33
- if (config.fields) {
34
- forIn(config.fields, (field_config, field_name) => {
35
- this.addField(field_name, field_config);
36
- });
37
- }
38
- }
39
-
40
- isValid(group_name?: string) {
41
- let field_names = Object.keys(this.fields);
42
- if (group_name) {
43
- field_names = Object.keys(this.groups[group_name]!.fields);
44
- }
45
-
46
- return field_names.every((field) => {
47
- return this.fields[field]!.errors.length === 0;
48
- });
49
- }
50
-
51
- reset(group_name?: string) {
52
- this.validated = false;
53
- this.errors = [];
54
-
55
- if (group_name) {
56
- forIn(this.groups[group_name]!.fields, (field) => {
57
- field.reset();
58
- });
59
- } else {
60
- forIn(this.fields, (field) => {
61
- field.reset();
62
- });
63
- }
64
- }
65
-
66
- addField(field_name: string, field_config: FieldValidatorConfig) {
67
- const new_field = new FieldValidator(field_config, this);
68
- this.fields[field_name] = new_field;
69
-
70
- if (field_config.group) {
71
- if (!this.groups[field_config.group]) {
72
- this.groups[field_config.group] = {
73
- dirty: false,
74
- fields: reactive({}),
75
- touched: false,
76
- validated: false,
77
- };
78
- }
79
- this.groups[field_config.group]!.fields[field_name] = new_field;
80
- }
81
- }
82
-
83
- removeField(field_name: string) {
84
- if (this.fields[field_name]!.group) {
85
- delete this.groups[this.fields[field_name]!.group]!.fields[field_name];
86
- }
87
- delete this.fields[field_name];
88
- }
89
-
90
- async validate(group_name?: string) {
91
- if (group_name && this.groups[group_name]) {
92
- const group = this.groups[group_name];
93
-
94
- await asyncForEach(Object.keys(group.fields), async (field) => {
95
- await group.fields[field]!.validate();
96
- });
97
-
98
- this.groups[group_name].validated = true;
99
- } else {
100
- await asyncForEach(Object.keys(this.fields), async (field) => {
101
- await this.fields[field]!.validate();
102
- });
103
- this.validated = true;
104
- }
105
- }
106
- }
package/src/index.ts DELETED
@@ -1,6 +0,0 @@
1
- import { Validator } from './ValidatorClass';
2
- import { FieldValidator } from './FieldValidatorClass';
3
- import FieldValidation from './FieldValidation.vue';
4
- import * as rules from './rules';
5
-
6
- export { Validator, FieldValidator, FieldValidation, rules };
@@ -1,3 +0,0 @@
1
- import { match } from './match';
2
-
3
- export { match };
@@ -1,8 +0,0 @@
1
- export async function match(match_value: any, message: string) {
2
- return function (value: any) {
3
- if (!match_value || value === match_value) {
4
- return null;
5
- }
6
- return message || 'The value does not match';
7
- };
8
- }