1.0.6 • Published 3 years ago

check-validation v1.0.6

Weekly downloads
149
License
MIT
Repository
github
Last release
3 years ago

check-validation

- Check validation of Object
- Please let me know issues to my github. if you find bugs :)
- Please let me know some weird text and error message. My english is not good
- Thank you Enjoy!

installing

npm:

npm install check-validation

yarn:

yarn add check-validation

import

import validation from "check-validation" 

var validation = require("check-validation")

Usage

  • Example Data
const data = {
    name: "john",
    email: "test@test.com",
    password: "code123",
    passwordConfirm: "code123",
    gender: "male",
    age: 25,
    hobby: "sports"
}
  • Simple
 let rules = {
     name : "required", // rules for name
     email: "required|email", // rules for email
     age : "number|min[20]|max[30]", // rules for age
     hobby: "required|minLength[2]", // rules for hobby
     password: "required|match[passwordConfirm]", // rules for password
}

const check = validation(data, rules) // {result : true or false , message: error message }

if (check.result) {
    //success
} else {
    //fail
    //console.log(check.message)
}
  • For Custom Error Message

    - if use title inner name object and then change the Subject

    - it must be use with rules

 let rules = {
     name : {
         rules : "required", // rules for name
         title : "my name" // change the key  "name" to "my name" for error message
     }
}
  • Full Custom Error Message

    - if use API names with rules an inner object it will be changing full Error Message

    - Api name works with priority than title

let rules = {
    name: {
        rules: "required", // rules for name
        required: "required error message of name"
    },
    email: {
        rules: "required|email", // rules for email
        title: "email2", // change the key name "email" to "email2" for error message
        email: "email form error message of email" // it does not care title value
    },
    age: {
        rules: "number|min[20]|max[29]", // rules for age
        number: "number error message of age",
        min: "min error message of age",
        max: "max error message of age"
    },
    hobby: {
        rules: "required|minLength[2]", // rules for hobby
        title: "my hobby",
        minLength: "minLength error message of hobby"
    },
    password : {
        rules : "required|match[passwordConfirm]", // rules for password
        title : "code",
        match : "match error message of password"
    }
}
  • For Korean

    - 세번째 파라미터에 ko 스트링을 넣을시에 에러메세지를 한글로 받아 볼 수 있음

    - 타이틀값에 단어만 넣으면 "을/를", "이/가" 는 자동으로 붙음

    - phone api 사용시 한국 휴대전화번호 형식을 체크함

let rules = {
    name: {
        rules: "required", // rules for name
        title : "이름" // 오류 예) 이름을 입력 바랍니다 
    },
    email: {
        rules: "required|email", // rules for email
        title: "나의 이메일", // 오류 예) 나의 이메일이 형식에 맞지 않습니다
    }
} 
const check = validation(data, rules, "ko")

if (check.result) {
    //success
} else {
    //fail
    //console.log(check.message)
}

#API

name & usagedescription
requiredcheck blank, null, undefined
emailcheck email format
numbercheck number
phonecheck cell phone format (in Korea)
koreancheck Korean
englishcheck English
englishNumbercheck English and Number
matchotherValueInObjectcheck match with other value
minnumberminimum value
maxnumbermaximum value
lengthnumberlengths of string
minLengthnumberminimum lengths of string
maxLengthnumbermaximum lengths of string