Skip to content

Commit fcb9edc

Browse files
committed
First release
0 parents  commit fcb9edc

File tree

9 files changed

+18974
-0
lines changed

9 files changed

+18974
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.DS_Store
2+
node_modules
3+
4+
# Log files
5+
npm-debug.log*
6+
yarn-debug.log*
7+
yarn-error.log*
8+
pnpm-debug.log*
9+
10+
# Editor directories and files
11+
.idea
12+
.vscode
13+
*.suo
14+
*.ntvs*
15+
*.njsproj
16+
*.sln
17+
*.sw?

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# **Validating ref**
2+
Accepts this rules
3+
```typescript
4+
'required' | 'email' | 'numeric' | 'date' | 'minLength' | 'maxLength';
5+
```
6+
Returns an object
7+
```typescript
8+
return reactive({
9+
model, // Value that you must assign to v-model
10+
isValid, // Bacome true if all rules becomes true
11+
modelRules, // An object that have a rules state,
12+
// For example, { required: true, minLength: false }
13+
});
14+
```
15+
16+
Test it: [Demo](https://alisherari.github.io/simple-vue3-validation/)
17+
18+
## **Installation**
19+
```shell
20+
npm install -S light-vue3-validation
21+
```
22+
23+
## **How to use it**
24+
25+
### **First**
26+
Import `validateRef` function
27+
```javascript
28+
import { validateRef } from "light-vue3-validation"
29+
```
30+
31+
### **Second**
32+
Assign this function to your variable and put two parameters
33+
* First is your model
34+
* Second is your validations
35+
```javascript
36+
setup() {
37+
...
38+
const phone = validateRef('', ['required', 'numeric', { type: "maxLength", value: 12 }])
39+
...
40+
}
41+
```
42+
If your validating data is an object do something like this
43+
```javascript
44+
import { reactive } from "vue"
45+
46+
const user = reactive({
47+
phone: validateRef('', ['required', { type: 'minLength', value: 12 }]),
48+
password: validateRef('', ['required', { type: 'minLength', value: 6 }]),
49+
remember_me: ref(true),
50+
})
51+
```
52+
53+
### **Third**
54+
Assign variable's `model` value to your `v-model`
55+
```html
56+
<input ... v-model="phone.model" ... />
57+
```
58+
59+
## That's all
60+
61+
Now you can control you variable and set messages you need by `modelRules` object parameters

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ["@vue/cli-plugin-babel/preset"],
3+
};

dist/index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.validateRef = void 0;
4+
var vue_demi_1 = require("vue-demi");
5+
var rulesList = {
6+
// Check if field is required (is not empty)
7+
required: function (value) { return !!value; },
8+
// Check if value is not less than minimal length
9+
minLength: function (value, minLength) { return value.length >= minLength; },
10+
// Check if value is not more than max length
11+
maxLength: function (value, maxLength) { return value.length <= maxLength; },
12+
// Test value for email by regular expression
13+
email: function (value) { return /^\w+@[a-zA-Z_]+?\.?[a-z]+\.[a-zA-Z]{2,10}$/.test(value); },
14+
// Check if value is a number
15+
numeric: function (value) { return !isNaN(value); },
16+
// Check if value is a Date
17+
date: function (value) {
18+
var valueDate = new Date(value);
19+
var timeInMilliSecond = valueDate.valueOf();
20+
var isInvalidDate = isNaN(timeInMilliSecond);
21+
return !isInvalidDate;
22+
}
23+
};
24+
// Validate your variable and make it reactive
25+
function validateRef(model, rules) {
26+
// TODO: I can't type it. If you know how to do it please do it
27+
var localRules = {};
28+
// Get input rules from global rules list and copy them to local rules
29+
rules.forEach(function (rule) {
30+
if (typeof rule === 'string') {
31+
localRules[rule] = rulesList[rule];
32+
}
33+
else {
34+
localRules[rule.type] = {
35+
type: rulesList[rule.type],
36+
rule: rule.value
37+
};
38+
}
39+
});
40+
var value = (0, vue_demi_1.ref)(model);
41+
var modelRules = (0, vue_demi_1.ref)({});
42+
var isValid = (0, vue_demi_1.ref)(false);
43+
(0, vue_demi_1.watch)(value, function () {
44+
// Loop through local rules array and run validation functions
45+
for (var rule in localRules) {
46+
if (typeof localRules[rule] === 'object') {
47+
modelRules.value[rule] = localRules[rule].type(value.value, localRules[rule].rule);
48+
}
49+
else {
50+
modelRules.value[rule] = localRules[rule](value.value);
51+
}
52+
}
53+
// Check if all rules are valid
54+
isValid.value = Object.values(modelRules.value).every(function (rule) { return rule; });
55+
}, { immediate: true });
56+
return (0, vue_demi_1.reactive)({
57+
model: value,
58+
isValid: isValid,
59+
modelRules: modelRules
60+
});
61+
}
62+
exports.validateRef = validateRef;

dist/index.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { reactive, Ref, ref, UnwrapRef, watch } from "vue-demi";
2+
import { IRulesList, TInputRule, TModelRule } from "./types"
3+
4+
const rulesList: IRulesList = {
5+
// Check if field is required (is not empty)
6+
required: value => !!value,
7+
8+
// Check if value is not less than minimal length
9+
minLength: (value, minLength) => value.length >= minLength,
10+
11+
// Check if value is not more than max length
12+
maxLength: (value, maxLength) => value.length <= maxLength,
13+
14+
// Test value for email by regular expression
15+
email: value => /^\w+@[a-zA-Z_]+?\.?[a-z]+\.[a-zA-Z]{2,10}$/.test(value),
16+
17+
// Check if value is a number
18+
numeric: value => !isNaN(value),
19+
20+
// Check if value is a Date
21+
date: value => {
22+
const valueDate = new Date(value);
23+
const timeInMilliSecond = valueDate.valueOf();
24+
const isInvalidDate = isNaN(timeInMilliSecond);
25+
return !isInvalidDate;
26+
},
27+
};
28+
29+
// Validate your variable and make it reactive
30+
export function validateRef(model: string | number, rules: TInputRule[]): UnwrapRef<any> {
31+
// TODO: I can't type it. If you know how to do it please do it
32+
const localRules: Record<string, any> = {};
33+
34+
// Get input rules from global rules list and copy them to local rules
35+
rules.forEach(rule => {
36+
if (typeof rule === 'string') {
37+
localRules[rule] = rulesList[rule];
38+
} else {
39+
localRules[rule.type] = {
40+
type: rulesList[rule.type],
41+
rule: rule.value,
42+
};
43+
}
44+
});
45+
46+
const value: Ref = ref<typeof model>(model);
47+
const modelRules: Ref = ref<Record<TModelRule, boolean> | {}>({});
48+
const isValid: Ref = ref<boolean>(false);
49+
50+
watch(
51+
value,
52+
() => {
53+
// Loop through local rules array and run validation functions
54+
for (const rule in localRules) {
55+
if (typeof localRules[rule] === 'object') {
56+
modelRules.value[rule] = localRules[rule].type(value.value, localRules[rule].rule);
57+
} else {
58+
modelRules.value[rule] = localRules[rule](value.value);
59+
}
60+
}
61+
// Check if all rules are valid
62+
isValid.value = Object.values(modelRules.value).every(rule => rule);
63+
},
64+
{ immediate: true }
65+
);
66+
67+
return reactive({
68+
model: value,
69+
isValid,
70+
modelRules,
71+
});
72+
}

dist/types.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export interface IRulesList {
2+
required?: (value: string) => boolean;
3+
minLength?: (value: string, minLength: number) => boolean;
4+
maxLength?: (value: string, maxLength: number) => boolean;
5+
email?: (value: string) => boolean;
6+
numeric?: (value: number) => boolean;
7+
date?: (value: string) => boolean;
8+
}
9+
10+
// TODO: If you need new rules you can add it yourself or write me on Telegram @alisher_aripov11
11+
export type TInputRule =
12+
| "required"
13+
| "email"
14+
| "numeric"
15+
| "date"
16+
| { type: "minLength" | "maxLength"; value: number };
17+
export type TModelRule =
18+
| "required"
19+
| "email"
20+
| "numeric"
21+
| "date"
22+
| "minLength"
23+
| "maxLength";
24+
25+
export { validateRef } from "./index"

0 commit comments

Comments
 (0)