README
Form data validator for web application or API
Inspired from Laravel validation.
Dependency free.
If you're building application with ExpressJs and MongoDB then this is definitely something you need to check out. This is a validation tool that you can use in your every day life as The following is also applicable to the whole javascript ecosystem in general
Summary
- Installation
- Validation rules
- Usage on CommonJS
- Usage with VueJS
- Usage with Express/MongoDB
- with repository pattern
- Hooks
- TODO
- Contribute
Installation
// with yarn
yarn add @bcdbuddy/validator
// with npm
npm install @bcdbuddy/validator
// typings: This package comes with its own typings so you won't need to install @types/...
Validation rules
- required
- confirmed
- unique
- exists
- required_unless
- required_with
- in_array
- boolean
Numeric
- greater_than
- lesser_than
- min
- max
- between
Date
- date
- after
- regex
String
- min_length
- max_length
- between_length
Usage on CommonJS
const Validator = require('@bcdbuddy/validator')
const v = await Validator.make({
data: {
name: 'John Doe',
age: 20,
email: 'john at domain dot com'
},
rules: {
name: 'required',
age: 'min:18',
email: 'required|email'
}
})
if (v.fails()) {
const errors = v.getErrors()
// {
// "email": "'john at domain dot com' is not a valid email"
// }
}
Usage with VueJS
// component.vue => template
<template lang="pug">
.register-page
h1 Créer votre compte
form.form(action="/user" method='POST' @submit.prevent="onSubmit")
.form-field
label(for="phone_number") Phone number
input(type="text" name="phone_number" id="phone_number" v-model="user.phone_number")
.form-field
label(for="password") Password
input(type="password" name="password" id="password" v-model="user.password")
.form-field
label(for="password_confirmation") Password confirmation
input(type="password" name="password_confirmation" id="password_confirmation" v-model="user.password_confirmation")
.form-actions
button.button(type="submit") Register
p
a(href="/user/login") Already a member ? Login here
</template>
// component.vue => script
import Validator from '@bcdbuddy/validator'
export default {
data () {
return {
user: {
phone_number: '',
password: '',
password_confirmation: '',
}
}
},
methods: {
async onSubmit () {
const v = await Validator.make({
data: this.user,
rules: {
phone_number: 'required|regex:^[0-9]{9,10}