Last active
March 31, 2020 18:13
-
-
Save Akifcan/de1a19d277acf08b018e68e012cac5a3 to your computer and use it in GitHub Desktop.
correctjs example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { correct } from 'correctjs' | |
or | |
const correct = require('correctjs') | |
const name = '' | |
const email = 'test@hotmail' | |
const age = '109a' | |
const customReg = '44' | |
const point = 20 | |
const sayHi = 'hello' | |
const lentest = 'tttttt' | |
let check = correct( | |
[ | |
{ | |
value: name, | |
fieldName: 'name', | |
validations: [ | |
{ | |
'type': 'required', | |
}, | |
{ | |
'type': 'min|5', | |
'message': 'name must be at least 5 character', | |
}, | |
{ | |
'type': 'max|15', | |
'message': 'name not be greater than 15 character' | |
}, | |
] | |
}, | |
{ | |
value: email, | |
fieldName: 'email', | |
validations: [ | |
{ | |
'type': 'required', | |
'message': 'email required' | |
}, | |
{ | |
'type': 'max|20', | |
'message': 'email must be at least 5 character' | |
}, | |
{ | |
'type': 'email', | |
'message': 'email address format wrong' | |
} | |
] | |
}, | |
{ | |
value: age, | |
fieldName: 'age', | |
validations: [ | |
{ | |
'type': 'required', | |
'message': 'age is required' | |
}, | |
{ | |
'type': 'number', | |
'message': 'age must be number' | |
}, | |
] | |
}, | |
{ | |
value: customReg, | |
fieldName: 'custom regex', | |
validations: [ | |
{ | |
'type': 'required', | |
'message': 'please enter phone number' | |
}, | |
{ | |
'type': 'customRegex|^\\d+$', | |
'message': 'custom regex' | |
}, | |
] | |
}, | |
{ | |
value: point, | |
fieldName: 'point', | |
validations: [ | |
{ | |
'type': 'between|10-30', | |
'message': 'point must be between 10 and 20' | |
}, | |
] | |
}, | |
{ | |
value: sayHi, | |
fieldName: 'sayhi', | |
validations: [ | |
{ | |
'type': 'required', | |
'message': 'sayhi required' | |
}, | |
{ | |
'type': 'startsWith|h', | |
'message': 'must start with h' | |
}, | |
] | |
}, | |
{ | |
value: sayHi, | |
fieldName: 'sayHi', | |
validations: [ | |
{ | |
'type': 'required', | |
'message': 'sayhi required' | |
}, | |
{ | |
'type': 'endsWith|o', | |
'message': 'must end with o' | |
}, | |
] | |
}, | |
{ | |
value: lentest, | |
fieldName: 'len test', | |
validations: [ | |
{ | |
'type': 'matchLength|6', | |
'message': 'lentest must be 6 character' | |
}, | |
] | |
}, | |
] | |
) | |
if(check == true){ | |
console.log('OK!') | |
}else{ | |
console.log(check) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment