Giter VIP home page Giter VIP logo

sployad / validue Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 2.18 MB

This library based on "class-validator" , it will help to easy you validate your fields, forms and etc. All you need to use decorators @PropertyValidate, @ActionValidate and all decorators from "class-validator" like @Max, @IsEmail and more...

JavaScript 3.56% HTML 3.52% Vue 20.97% TypeScript 71.96%
vue vuejs class-validator decorators vue-typescript validate-js typescript validation validation-library

validue's Introduction

enter image description here

This library is based on the "class-validator" library. It will help you easily validate your fields, forms and etc.. All you need to use is @PropertyValidator , @ErrorValidator, @ActionValidator decorators, and all decorators from “class validator” such as @Max, @IsEmail and etc..

Instalation

npm i validue

Demo

Go to the link bellow for watch demo

Link demo on the codesandbox

Example

Validation errors check automatically because Validue create a Vue Watcher for watching field editing. This example close to the real task, all you need to append for work it's a business logic in the SignUp method

<template>
  <form>
    <div class="text-field">
      <label class="text-field__label" for="username"> Your username </label>
      <input class="text-field__input" id="username" placeholder="Enter username">
      <p class="text-field__error" v-if="usernameErrors.isNotEmpty || usernameErrors.length">
        {{usernameErrors.isNotEmpty || usernameErrors.length}} 
      </p>
    </div>
    <div class="text-field">
      <label class="text-field__label" for="email"> Your email </label>
      <input class="text-field__input" id="email" placeholder="Enter email">
      <p class="text-field__error" v-if="emailErrors.isNotEmpty || emailErrors.isEmail">
        {{emailErrors.isNotEmpty || emailErrors.isEmail}}
      </p>
    </div>
    <button @click="signUp"> Sign up </button>
  </form>
</template>
<script lang="ts">
import {Component, Vue} from "vue-property-decorator";
import {ActionValidator, PropertyValidator, ErrorValidator, IsNotEmpty, IsEmail, Length} from "validue";

@Component({})
export default class App extends Vue {

  @IsNotEmpty({message: "Required field"})
  @Length(1, 10, {message: "Field more then 10 chars or less then 1 char"})
  username = "";

  @IsNotEmpty({message: "Required field"})
  @IsEmail({}, {message: "Wrong email address"})
  email = "";

  @PropertyValidator("username")
  usernameErrors = {};

  @PropertyValidator("email")
  emailErrors = {};
  
  @ActionValidator()
  signUp(@ErrorValidator errors: []){
    if(errors.length){
      console.log('U have some errors');
      return;
    }
    //... your business logic
  }
}
</script>

Usage

@PropertyValidator has 3 override syntax:

PropertyValidator(path: string, validationFunctions?: Function[], options?: WatchOptions)

PropertyValidator(path: string, validationFunctions?:Function[])

PropertyValidator(path: string, options?: WatchOptions)

You need to add this decorator before the field, errors in which are written after validation. First argument is a field that is watched. It works like @Watch decorator in Vue. Second argument receives an array of validation functions. This argument is not required because you can use 2 methods to declare validation of your fields.

All decorators of "class-validator" in here

Example: 1 Way:

    import {Component, Vue} from "vue-property-decorator";    
    import {ActionValidator, PropertyValidator,IsNotEmpty, IsEmail, Length} from "validue";  
      
    @Component({})  
    export default class App extends Vue {  
      
	    @IsNotEmpty({message: "Required field"})  
	    @Length(1, 10, {message: "Field more then 10 chars or less then 1 char"})  
	    firstName = "";  
  
	    @IsNotEmpty({message: "Required field"})  
	    @IsEmail({}, {message: "Wrong email address"})  
	    email = "";  
  
	    @PropertyValidator("firstName")  
	    firstNameErrors = {};  
  
	    @PropertyValidator("email")  
	    emailErrors = {};  
    }

2 Way:

  
   import {Component, Vue} from "vue-property-decorator";  
   import {ActionValidator, PropertyValidator,IsNotEmpty, IsEmail, Length} from "validue";  
     
   @Component({})  
   export default class App extends Vue {  
     
       firstName = "";  
       email = "";  
     
       @PropertyValidator("firstName", [  
           IsNotEmpty({message: "Required field"}),  
           Length(1, 10, {message: "Field more then 10 chars or less then 1 char"})  
       ])  
       firstNameErrors = {};  
     
       @PropertyValidator("email", [  
           IsNotEmpty({message: "Required field"}),  
           IsEmail({}, {message: "Wrong email address"})  
       ])  
       emailErrors = {};  
   }

@ActionValidator(group?: string[])

This decorator is added before the method. Thus, before the method is called, all fields and field groups are validated, and returned errors are written in fields with @ProperyValidator added before. Example without group:

    import {Component, Vue} from "vue-property-decorator";   
    import {ActionValidator, PropertyValidator, IsNotEmpty, IsEmail, Length} from "validue";  
      
    @Component({})  
    export default class App extends Vue {  
      
        @IsNotEmpty({message: "Required field"})  
        @Length(1, 10, {message: "Field more then 10 chars or less then 1 char"})  
        firstName = "";  
      
        @IsNotEmpty({message: "Required field"})  
        @IsEmail({}, {message: "Wrong email address"})  
        email = "";  
      
        @PropertyValidator("firstName")  
        firstNameErrors = {};  
      
        @PropertyValidator("email")  
        emailErrors = {};  
      
        @ActionValidator()  
        send(){  
            console.log(this.firstNameErrors, this.emailErrors);  
        }  
    } 

Example with group:

    import {Component, Vue} from "vue-property-decorator";
import {ActionValidator, PropertyValidator, IsNotEmpty, IsEmail, Length} from "validue";

@Component({})
export default class App extends Vue {

    @IsNotEmpty({message: "Required field",
        groups: ['registration']})
    @Length(1, 10, {message: "Field more then 10 chars or less then 1 char",
        groups: ['registration']})
    firstName = "";

    @IsNotEmpty({message: "Required field",
        groups: ['registration']})
    @Length(1, 10, {message: "Field more then 10 chars or less then 1 char",
        groups: ['registration']})
    lastName = "";

    @PropertyValidator("firstName")
    firstNameErrors = {};

    @PropertyValidator("lastName")
    lastNameErrors = {};

    @IsNotEmpty({message: "Required field",
        groups: ['auth']})
    @IsEmail({}, {message: "Wrong email address", groups: ['auth']})
    email = "";

    @IsNotEmpty({message: "Required field",
        groups: ['auth']})
    @IsEmail({}, {message: "Wrong email address", groups: ['auth']})
    password = "";

    @PropertyValidator("email")
    emailErrors = {};

    @PropertyValidator("password")
    passwordErrors = {};

    @ActionValidator(['registration'])
    register(){
        //Will validate fields which have group 'registration'  
    }

    @ActionValidator(['auth'])
    auth(){
        //Will validate fields which have group 'auth'  
    }
} 

@ErrorValidator paramName

This decorator is added in method's param and after call the method in this variable will write all need errors ('need' cuz u can use Groups, see upper)

import {Component, Vue} from "vue-property-decorator";
import {ActionValidator, PropertyValidator, IsNotEmpty, IsEmail, Length} from "validue";
import {ErrorValidator} from './validue-decorators';

@Component({})
export default class App extends Vue {

    @IsNotEmpty({message: "Required field"})
    @Length(1, 10, {message: "Field more then 10 chars or less then 1 char"})
    firstName = "";

    @IsNotEmpty({message: "Required field"})
    @IsEmail({}, {message: "Wrong email address"})
    email = "";

    @PropertyValidator("firstName")
    firstNameErrors = {};

    @PropertyValidator("email")
    emailErrors = {};

    @ActionValidator()
    send(@ErrorValidator errors) {
        console.log(errors) // errors = [ {firstNameErrors: {...errors-here...}}, {emailErrors: {...errors-here...}}]
    }
} 

validue's People

Contributors

sployad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.