0.0.83 • Published 4 months ago

@gnx-utilities/validators v0.0.83

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

📝 Fluent Validations

Fluent Validations is a library that allows you to create validations for your entities and properties in a simple way.

📦 Installation

npm install @gnx-utilities/validators
pnpm add @gnx-utilities/validators
yarn add @gnx-utilities/validators
bun add @gnx-utilities/validators

📖 Usage

import { FluentValidation } from '@gnx-utilities/validators'

const email =  'test@test.test'

const validation = FluentValidation
    .create()
    .for({value: email })
    .isRequired()
    .isEmail()


console.log(validation.validate()) // true

console.log(validation.softValidation()) // { isValid: true, errors: [], totalErrors: 0 }

console.log(validation.getErrors()) // []

const wrongValidation = FluentValidation
    .create()
    .for({value: 'John Doe' })
    .isEmail()

console.log(wrongValidation.validate()) // false

console.log(wrongValidation.softValidation()) // { isValid: false, errors: [ 'The value is not a valid email' ], totalErrors: 1 } 

console.log(wrongValidation.getErrors()) // [ 'The value is not a valid email' ]
import { ModelValidator } from '@gnx-utilities/validators'

const user = {
    name: 'John',
    email: 'test@test.test',
    age: 20
}
const validation = ModelValidator
    .create()
    .for({model: user })
    .withProperty(({ name }) => name === 'John')
    .withProperty(({ email }) => email === 'test@test.test')
    .withProperty(({ age }) => age === 20)

console.log(validation.validate()) // true

console.log(validation.softValidation()) // { isValid: true, errors: [], totalErrors: 0 }

console.log(validation.getErrors()) // []

const wrongValidation = ModelValidator
    .create()
    .for({model: user })
    .withProperty(({ name }) => name === 'Jane')
    .withProperty(({ email }) => email === 'testing@test.com')
    .withProperty(({ age }) => age === 50)

console.log(wrongValidation.validate()) // false

console.log(wrongValidation.softValidation()) // { isValid: false, errors: [ 'The property name is not valid', 'The property email is not valid', 'The property age is not valid' ], totalErrors: 3 }

console.log(wrongValidation.getErrors()) // [ 'The property name is not valid', 'The property email is not valid', 'The property age is not valid' ]

📚 Documentation

🛠️ Tools

Typescript NodeJS

Authors

ImRLopezAG

🔗 Links

portfolio linkedin twitter

0.0.8

4 months ago

0.0.81

4 months ago

0.0.82

4 months ago

0.0.83

4 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.6

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago