2.3.0 • Published 5 years ago

@logicroom/validator v2.3.0

Weekly downloads
332
License
MIT
Repository
github
Last release
5 years ago

Validator

This is the validation script for The Clean Architecture (JavaScript)

Getting Started

  npm install --save @logicroom/validator

or

  yarn add @logicroom/validator

Purpose

The purpose of the Logic Room Validator is simplify adding validation and change handling to form inputs when using Mobx. The library exposes two new classes GenericFormInputPresenter and GenericFormPresenter. The former provides an intelligent form input API that can be extended to create view model code for any form input (e.g a TextInput). The latter is for grouping these inputs into a form and provides form level attributes and methods.

GenericFormInputPresenter

API

You create a new form input by newing up the class:

import { GenericFormInputPresenter } from '@logicroom/validator'
const emailInput = new GenericFormInputPresenter<string>('email@address.com')

In the above example, a new text input has been created. We've told the GenericFormInputPresenter that the initial value is 'email@address.com'. We've also said that the value is of type 'string' passed as a generic in <string>.

This gives us access to the following properties and methods on the emailInput instance. All properties are observable via Mobx.

NameProperty/MethodValueDefaultType
valuePropertycurrent value of inputinitial value provided at constructiongeneric (provided at construction)
isValidPropertywhether the current value passes all current validation rulestrueboolean
errorMessagesPropertya list of messages for all currently non-passing rules[]string[]
isDirtyPropertywhether value has been changed via onChange handlerfalseboolean
disabledPropertywhether the form input is disabledfalseboolean
requiredPropertywhether the form input is requiredfalseboolean
onChangeMethodthe function to call to update the value (to be bound in the view layer)(newValue: T) => void
withMiddlewareMethodthis adds a middleware that can be used to access and manipulate the value coming into onChange before it gets set to value (e.g. v => v.toUpperCase())(newValue: T) => T
resetMethodsets the value and isDirty back to their defaults() => void
addCustomRuleMethod (fluent)use this method to add a custom validation rule(condition: (value: T) => boolean, errorMessage?: string) => this
mustBeEmailMethod (fluent)value must conform to the General Email Regex found hereunset(errorMessage?: string) => this
mustBeTrueMethod (fluent)value must be trueunset(errorMessage?: string) => this
mustBeBoolMethod (fluent)value must be true or falseunset(errorMessage?: string) => this
mustBeStringMethod (fluent)value must be of type stringunset(errorMessage?: string) => this
mustBeNumberPrimitiveMethod (fluent)value must be of type numberunset(errorMessage?: string) => this
mustBeNumberMethod (fluent)value must be of type number or valid number string (e.g. '55')unset(errorMessage?: string) => this
minLengthMethod (fluent)value must have length equal or greater than provided numberunset(length: number, errorMessage?: string) => this
maxLengthMethod (fluent)value must have length equal or less than provided numberunset(length: number, errorMessage?: string) => this
isRequiredMethod (fluent)value must be populatedunset(errorMessage?: string) => this
isDisabledMethod (fluent)input is disabledunset() => this

Examples

Creation

const emailInput = new GenericFormInputPresenter<string>('email@address.com')
  .mustBeEmail()
  .isRequired()

Update value

emailInput.onChange('new@value.com`)

Check validity

const emailInput = new GenericFormInputPresenter<string>('email@address.com')
  .mustBeEmail('Your email address is not valid')
  .isRequired()

console.log(emailInput.isValid)
// true

emailInput.onChange('qwerty')

console.log(emailInput.isValid)
// false

console.log(emailInput.errorMessages)
// ['Your email address is not valid']

GenericFormPresenter

API

You create a new form by newing up the class:

import {
  GenericFormInputPresenter,
  GenericFormPresenter
} from '@logicroom/validator'

// first we create the input(s) that will be used by the form
const emailInput = new GenericFormInputPresenter<string>('email@address.com')

// then we create the form and add our form input(s)
const myForm = new GenericFormPresenter().addFormInput(emailInput)

In the above example a new form is created with GenericFormPresenter. We've told our form that it has one form input (emailInput). The below table describes the properies and methods available on myForm.

NameProperty/MethodValueDefaultType
isDirtyPropertyform is dirty if any of the inputs are dirtyfalseboolean
isValidPropertyform is valid if all of the inputs are valid and no server errors existtrueboolean
serverErrorsPropertyset server errors to indicate validation errors that have been returned server side validation[]string[]
errorMessagesPropertyany server errors concatanated with any errors from invalid form inputs[]string[]
addFormInputMethod (fluent)call this method to add a form input to this form(input: GenericFormInputPresenter) => this
resetFormMethodcall this method to reset each input in the form() => void

Examples

Creation

const emailInput = new GenericFormInputPresenter<string>('')
const passwordInput = new GenericFormInputPresenter<string>('')

const myForm = new GenericFormPresenter()
  .addFormInput(emailInput)
  .addFormInput(passwordInput)

Reset Form

const emailInput = new GenericFormInputPresenter<string>('intial')
const passwordInput = new GenericFormInputPresenter<string>('')

const myForm = new GenericFormPresenter()
  .addFormInput(emailInput)
  .addFormInput(passwordInput)

emailInput.onChange('foo')

console.log(emailInput.value)
// 'foo'

myForm.reset()

console.log(emailInput.value)
// 'intial'

Form validity / server errors

const emailInput = new GenericFormInputPresenter<string>(
  'test@email.com'
).mustBeEmail('Must be email')

const myForm = new GenericFormPresenter().addFormInput(emailInput)

console.log(myForm.isValid)
// true

emailInput.onChange('not an email')

console.log(myForm.isValid)
// true

console.log(myForm.errors)
// ['Must be email']

myForm.serverErrors.push('Email not found, please register first')

console.log(myForm.errors)
// ['Must be email', 'Email not found, please register first']
2.3.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.5-alpha1

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago