Skip to content

Validators

📝 Fluent Validations

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

📦 Installation

Terminal window
npm install @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' ]