@splitty-test/validation 0.1.1 → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ronnie Garcia
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 CHANGED
@@ -1,39 +1,9 @@
1
1
  # validation
2
2
 
3
- This template should help get you started developing with Vue 3 in Vite.
3
+ This is a validation library for Vue 3 that is both simple but powerful. More documentation is coming soon.
4
4
 
5
- ## Recommended IDE Setup
5
+ ## Installation
6
6
 
7
- [VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (and disable Vetur).
8
-
9
- ## Type Support for `.vue` Imports in TS
10
-
11
- TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar) to make the TypeScript language service aware of `.vue` types.
12
-
13
- ## Customize configuration
14
-
15
- See [Vite Configuration Reference](https://vite.dev/config/).
16
-
17
- ## Project Setup
18
-
19
- ```sh
20
- npm install
21
- ```
22
-
23
- ### Compile and Hot-Reload for Development
24
-
25
- ```sh
26
- npm run dev
27
7
  ```
28
-
29
- ### Type-Check, Compile and Minify for Production
30
-
31
- ```sh
32
- npm run build
33
- ```
34
-
35
- ### Lint with [ESLint](https://eslint.org/)
36
-
37
- ```sh
38
- npm run lint
8
+ npm install @splitty-test/validation
39
9
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitty-test/validation",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "main": "dist/index.umd.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
package/.editorconfig DELETED
@@ -1,8 +0,0 @@
1
- [*.{js,jsx,mjs,cjs,ts,tsx,mts,cts,vue,css,scss,sass,less,styl}]
2
- charset = utf-8
3
- indent_size = 4
4
- indent_style = tab
5
- insert_final_newline = true
6
- trim_trailing_whitespace = true
7
- end_of_line = lf
8
- max_line_length = 100
@@ -1,8 +0,0 @@
1
- {
2
- "recommendations": [
3
- "Vue.volar",
4
- "dbaeumer.vscode-eslint",
5
- "EditorConfig.EditorConfig",
6
- "esbenp.prettier-vscode"
7
- ]
8
- }
@@ -1,118 +0,0 @@
1
- <template>
2
- <div ref="root" :class="{ invalid: error }">
3
- <slot :invalid="!!error" :error_message="error"></slot>
4
- </div>
5
- </template>
6
-
7
- <script lang="ts">
8
- import { defineComponent, nextTick, type PropType } from 'vue';
9
- import { Validator } from './ValidatorClass';
10
- import { isEqual, isPlainObject, remove } from 'lodash-es';
11
- import type { ValidationRule } from './FieldValidatorClass';
12
-
13
- export default defineComponent({
14
- name: 'FieldValidation',
15
- props: {
16
- name: {
17
- type: String,
18
- required: true,
19
- },
20
- hideError: Boolean,
21
- modelValue: {
22
- type: null,
23
- },
24
- modelModifiers: {
25
- default: () => ({}),
26
- },
27
- rules: {
28
- type: Array as PropType<ValidationRule[]>,
29
- default() {
30
- return [];
31
- },
32
- },
33
- validator: {
34
- type: Validator,
35
- required: true,
36
- },
37
- },
38
- data() {
39
- return {
40
- form_fields: [] as HTMLElement[],
41
- };
42
- },
43
- computed: {
44
- field() {
45
- return this.validator.fields[this.name]!;
46
- },
47
- error() {
48
- if (this.field && this.field.errors.length) {
49
- return this.field.errors[0];
50
- }
51
- return null;
52
- },
53
- },
54
- watch: {
55
- modelValue: {
56
- async handler(new_value: any, old_value: any) {
57
- if (!isEqual(new_value, old_value)) {
58
- if (this.field) {
59
- this.field.value = new_value;
60
- this.field.dirty = true;
61
- }
62
- this.validator.dirty = true;
63
- if (this.field.group) {
64
- this.validator.groups[this.field.group]!.dirty = true;
65
- }
66
- if (this.field.touched) {
67
- await this.field.validate();
68
- }
69
- }
70
- },
71
- deep: true,
72
- },
73
- },
74
- mounted() {
75
- // Add the field to the validator
76
- if (!this.field) {
77
- let group;
78
- if (Object.keys(this.modelModifiers).length === 1) {
79
- group = Object.keys(this.modelModifiers)[0];
80
- }
81
- this.validator.addField(this.name, {
82
- value: this.modelValue,
83
- rules: this.rules,
84
- group,
85
- });
86
- }
87
-
88
- nextTick(() => {
89
- // Add control elements to the validation methods
90
- const root = this.$refs.root as HTMLElement;
91
- const form_fields = root.querySelectorAll<HTMLElement>('input, select, textarea');
92
- this.form_fields = Array.from(form_fields);
93
- this.form_fields.forEach((form_field) => {
94
- if (form_field.getAttribute('has-validation') !== 'true') {
95
- this.validator.fields[this.name]?.controls.push(form_field);
96
- form_field.addEventListener('blur', (event: FocusEvent) => {
97
- if (!root.contains(event.relatedTarget as Node)) {
98
- this.validator.touched = true;
99
- if (this.validator.fields[this.name]!.group) {
100
- const group_name = this.validator.fields[this.name]!.group;
101
- this.validator.groups[group_name as string]!.touched = true;
102
- }
103
- this.validator.fields[this.name]!.touched = true;
104
- this.validator.fields[this.name]!.validate();
105
- }
106
- });
107
- form_field.setAttribute('has-validation', 'true');
108
- }
109
- });
110
- });
111
- },
112
- beforeUnmount() {
113
- this.form_fields.forEach((form_field) => {
114
- remove(this.validator.fields[this.name]!.controls, form_field);
115
- });
116
- },
117
- });
118
- </script>
@@ -1,74 +0,0 @@
1
- import { asyncForEach } from 'modern-async';
2
- import { type Validator } from './ValidatorClass';
3
-
4
- export interface FieldValidatorConfig {
5
- value: any;
6
- rules: ValidationRule[];
7
- group?: string;
8
- }
9
-
10
- export type ValidationRule = (value: any, ...args: any) => Promise<string | null> | (string | null);
11
-
12
- export class FieldValidator {
13
- controls: HTMLElement[];
14
- dirty: boolean;
15
- errors: string[];
16
- group?: string;
17
- rules: ValidationRule[];
18
- touched: boolean;
19
- validated: boolean;
20
- validator: Validator;
21
- value: any;
22
-
23
- constructor(config: FieldValidatorConfig, validator: Validator) {
24
- this.controls = [];
25
- this.dirty = false;
26
- this.errors = [];
27
- this.rules = config.rules;
28
- this.touched = false;
29
- this.validated = false;
30
- this.validator = validator;
31
- this.value = config.value;
32
-
33
- if (config.group) {
34
- this.group = config.group;
35
- }
36
- }
37
-
38
- get isValid() {
39
- return this.errors.length === 0;
40
- }
41
-
42
- reset() {
43
- this.touched = false;
44
- this.dirty = false;
45
- this.validated = false;
46
- this.errors = [];
47
- }
48
-
49
- async validate() {
50
- this.errors = [];
51
-
52
- // Only validate if the controls are not disabled or hidden
53
- let should_validate = false;
54
- if (
55
- this.controls.length > 0 &&
56
- this.controls?.every((control_element) => {
57
- return !control_element.getAttribute('disabled');
58
- })
59
- ) {
60
- should_validate = true;
61
- }
62
-
63
- if (should_validate) {
64
- await asyncForEach(this.rules, async (rule: ValidationRule) => {
65
- const error = await rule(this.value);
66
- if (error !== null) {
67
- this.errors.push(error);
68
- }
69
- });
70
- }
71
-
72
- return this.isValid;
73
- }
74
- }
@@ -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
- }