Socket
Socket
Sign inDemoInstall

vuelidate-property-decorators

Package Overview
Dependencies
12
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    vuelidate-property-decorators

A thin wrapper of vuelidate library to simplify its usage with vue-class-component.


Version published
Weekly downloads
4.2K
decreased by-14.49%
Maintainers
1
Install size
226 kB
Created
Weekly downloads
 

Readme

Source

vuelidate property decorators

This library provides a thin wrapper of vuelidate library to simplify its usage with vue-class-component or vue-property-decorator.

Installation

yarn add vuelidate-property-decorators

Usage

Set up vuelidate library as described in (https://vuelidate.netlify.com/#sub-installation).

Then on your component:

Validating single field

To set per-field validation, use the @Validate decorator:


import {Validate} from 'vuelidate-property-decorators';
import {required} from 'vuelidate/lib/validators'

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

    @Validate({required})
    firstName = '';

    @Validate({required})
    lastName = '';

}

Template (pug in this case) looks the same way as in pure vuelidate:

.form-group
    q-input(v-model="$v.firstName.$model")
    .error(v-if="!$v.firstName.required") Field is required

.form-group
    q-input(v-model="$v.lastName.$model")
    .error(v-if="!$v.lastName.required") Field is required

Setting validation for all fields at once

To set the validation for all fields at once, use @Validations decorator:


import {Validations} from 'vuelidate-property-decorators';
import {required} from 'vuelidate/lib/validators'

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

    firstName = '';
    lastName = '';

    @Validations()
    validations = {
        firstName: {required},
        lastName: {required}
    }

}

Dynamic validations

Both the argument of @Validate(...) and the value of @Validations() can be a function that is called (reactively) with this set to the component instance.

Example:


import {Validate, Validations} from 'vuelidate-property-decorators';
import {required} from 'vuelidate/lib/validators'

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

    firstName = '';
    lastName = '';

    isRequired = false;

    @Validations()
    validations() {
        if (this.isRequired) {
            return {
                firstName: {required},
                lastName: {required}
            }
        }
        return {}
    }
    
    @Validate(() => {
        if (this.isRequired) {
            return {required}
        }
        return {}
    })
    test = '';
}

Keywords

FAQs

Last updated on 29 Oct 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc