@splitty-test/validation 0.2.0 → 0.2.1

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 (2) hide show
  1. package/README.md +107 -3
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,9 +1,113 @@
1
1
  # validation
2
2
 
3
- This is a validation library for Vue 3 that is both simple but powerful. More documentation is coming soon.
3
+ This is a validation library for Vue 3 that is both simple but powerful.
4
4
 
5
- ## Installation
5
+ ## Install
6
6
 
7
+ Install with NPM:
8
+
9
+ ```
10
+ $ npm install @splitty-test/validation
7
11
  ```
8
- npm install @splitty-test/validation
12
+
13
+ ## Usage
14
+
15
+ Using Composition API...
16
+
17
+ In the script:
18
+
19
+ ```ts
20
+ <script setup>
21
+ import { ref } from 'vue';
22
+ import { FieldValidation, useFormValidator, rules } from '@splitty-test/validation';
23
+
24
+ const login_form_validator = useFormValidator();
25
+ const email = ref('');
26
+ const password = ref('');
27
+
28
+ async function handleFormSubmit() {
29
+ // Validate the form fields
30
+ const is_valid = await login_form_validator.validate();
31
+
32
+ if (is_valid) {
33
+ // DO THE REST OF THE FORM SUBMIT STUFF
34
+ }
35
+ }
36
+
37
+ export {
38
+ email,
39
+ password,
40
+ rules,
41
+ handleFormSubmit
42
+ }
43
+ </script>
44
+ ```
45
+
46
+ In the template:
47
+
48
+ ```html
49
+ <template>
50
+ <div class='login-form'>
51
+ <div class='email-field'>
52
+ <FieldValidation name="email" v-model="email" :validator="login_form_validator" :rules="[rules.required, rules.email]">
53
+ <input name="email" v-model="email">
54
+ </FieldValidation>
55
+ </div>
56
+ <div class='password-field'>
57
+ <FieldValidation name="password" v-model="password" :validator="login_form_validator" :rules="[rules.required]">
58
+ <input name="password" v-model="password">
59
+ </FieldValidation>
60
+ </div>
61
+ <div class='submit-button'>
62
+ <input type="button" @click="handleFormSubmit()">Login</input>
63
+ </div>
64
+ </div>
65
+ </template>
66
+ ```
67
+
68
+ ### FieldValidation Component Properties
69
+
70
+ - **name (String) - Required:** A unique name for the field that is used intenally to reference the field being validated.
71
+ - **v-model (any) - Required:** The value that is being validated.
72
+ - **validator (FormValidator) - Required:** The instance of the FormValidator that the field is attached to.
73
+ - **rules (Rule[]): - Conditionally Required** Rules are a list of functions used to validated the value in the v-model. It is only required on the FieldValidation component if they weren't defined when creating the FormValidator.
74
+
75
+ ### Making Custom Rules
76
+
77
+ Each rule is a simple function that takes the v-model value as the first argument and returns an error message string if there is an issue or `null` if the value passes validation. If a string message is returned, it will be added to the errors array. You can create custom rules with arguments as long as the function then returns a function with the value passed as the first argument. Make sure you are passing the rule definition (without parenthesis) to the rules array. You can use async functions as well.
78
+
79
+ Example:
80
+
81
+ ```ts
82
+ <script setup>
83
+ // The value has to equal 'foo'
84
+ function myCustomRule(value: string) {
85
+ if (value !== 'foo') {
86
+ return 'The value MUST equal foo!';
87
+ }
88
+ return null;
89
+ }
90
+
91
+ // The value must be with a range
92
+ function myCustomRuleWithArguments(min: number, max: number) {
93
+ return (value: number) => {
94
+ if (value < min || value > max) {
95
+ return `The value must be between ${min} and ${max}`;
96
+ }
97
+ return null;
98
+ };
99
+ }
100
+
101
+ export {
102
+ myCustomRule,
103
+ myCustomRuleWithArguments
104
+ }
105
+ </script>
106
+ ```
107
+
108
+ Then in the template:
109
+
110
+ ```html
111
+ <FieldValidation name="pity" v-model="who_do_i_pity" :validator="myValidator" :rules="myCustomRule">...</FieldVaidation>
112
+ <FieldValidation name="age" v-model="age" :validator="myValidator" :rules="myCustomRuleWithArguments(18, 50)">...</FieldVaidation>
9
113
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitty-test/validation",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "main": "dist/index.umd.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",