0.0.15 • Published 3 months ago

lightjx v0.0.15

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

LightJx - A Validation Framework

A javascript validation framework ported and adapted from LightVx

Usage

npm i lightjx --save
import { Validate } from 'lightjx';

let validator = Validate.field("MyField","Display Name").required().asPhoneNumber();
validator.validate("This is the user input");
if(!validator.isValid) {
    console.error(validator.errorMessage);
}

A note on security & validation

Remember that validation in the browser is purely asthetic for user convenience. It offers no security and is easily bypassed. All systems should appropriately validate in the backend as part of a trusted and secure process.

Examples

Storing all validation rules in an object using the name

{
    businessName: Validate.field("businessName", "Business Name").required().asAlphaNumericHyphenText(),
    email: Validate.field("email", "E-Mail").required().asEmail(),
    phone: Validate.field("phone", "Phone").asPhoneNumber(),
    fax: Validate.field("fax", "Fax").asPhoneNumber()
}

Using a validator directly

import { Validate } from 'lightjx';

let emailValidator = Validate.field("email", "E-Mail").required().asEmail().validate("yourEmail@address");
if(!emailValidator.isValid) {
    console.error(emailValidator.errorMessage);
}

Using a custom regular expression

This validator is not using the required validator, so if there is no input the isValid property will be true. Most validators will succeed if no user input unless .required() is used.

import { Validate } from 'lightjx';

let validator = Validate.field("MyField","Display Name").withExpression(/^[a-zA-Z0-9]{1,}$/);
validator.validate("This is the user input");
if(!validator.isValid) {
    console.error(validator.errorMessage);
}

Using a function to defer validtaion

Sometimes, you might want to validate using information from another source, such as a different field in the form. Some validators allow a function to be used instead of the comparison value, these are:

  • MinValidator
  • MaxValidator
  • MinDateValidator
  • BetweenDateValidator
  • ContainsTextValidator
  • NotContainsTextValidator
  • LengthValidator Passing in a function will allow more advanced validation that adjusts to changing form data.
import { Validate } from 'lightjx';

let validator = Validate.field("MyField","Display Name").min(() => yourFormState.values.minCapacity);
validator.validate(100);
if(!validator.isValid) {
    console.error(validator.errorMessage);
}

Validate.field and Validate.define

In general, if you want to create user friendly validation messages, use the Validate.field function as this allows you to pass in control and display name values. If you want to just create validation rules and ignore field names, you can use Validate.define

//Using field
let validator = Validate.field("username", "Your username").required().asAlphaText().hasMaxLength(5);
//Using Define
let validator = Validate.define().required().asAlphaText().hasMaxLength(5);

Available commands

  • with(validator:Validator)
  • withExpression(expression:string | RegExp)
  • asAlphaText()
  • asAlphaNumericText()
  • asAlphaNumericHyphenText()
  • asName()
  • asPhoneNumber()
  • asEmail()
  • asDate()
  • isDateOnOrAfter(minDate:Date)
  • isDateOnOrBefore(maxDate:Date)
  • isDateBetween(minDate:Date, maxDate:Date)
  • asBoolean()
  • containsText(searchText:string, ignoreCase:boolean = false)
  • asInt()
  • asFloat()
  • asGuid()
  • asHexColor()
  • in(items:Array)
  • notIn(items:Array)
  • isNull()
  • isEmptyString()
  • is(value:any)
  • isNot(value:any)
  • hasLengthRange(min?:number, max?:number)
  • hasLength(length:number)
  • hasNoBrackets()
  • min(min:number)
  • max(max:number)
0.0.15

3 months ago

0.0.14

5 months ago

0.0.13

10 months ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.10

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago