0.1.10 • Published 3 years ago

spl_validator v0.1.10

Weekly downloads
12
License
ISC
Repository
github
Last release
3 years ago

spl_validator

A simple and extensible validator for Node.js.

Installation

$ npm install --save spl_validator

Usage

const spl_validator = require('spl_validator')
const validator = new spl_validator()

try {
    let input =  {
        name: 'John Doe'
    };

    let ruleset = {
        name: { Required: true, MinLength: 5, MaxLength: 10 }
    }

    input = await validator.Validate(ruleset, input)

    console.log(input)
} catch (err) {
    console.error(err)
}

Available Rules

RuleNameDescriptionValue
RequiredInput must not emptyboolean
EmailInput must be valid email addressboolean
MinLengthInput must have length greater or equal than the specified valueinteger
MaxLengthInput must have length less or equal than the specified valueinteger
LengthInput must have length equal to the specified valueinteger
EqualToInput must have value equal to other input valueother input name
EnumInput must have value equal to one of the specified valuearray of values
IntegerInput must have integer valueboolean
MinInput must have value greater or equal than specified valueboolean
MaxInput must have value less or equal than specified valueboolean
LowercaseTransform input to lowercase stringboolean
UppercaseTransform input to uppercase stringboolean
DateInput must be a date string with given formatstring
RegExInput must be a regular expression stringstring

Ruleset From File

You can also store the ruleset in a .json, .js, or .txt file.

myruleset.json

{
    "agree": {
        "Required": true,
        "Enum": ["yes", "no"]
    }
}
const spl_validator = require('spl_validator')
const validator = new spl_validator()

try {
    let input =  {
        name: 'John Doe'
    };

    input = await validator.Validate('myruleset', input)

    console.log(input)
} catch (err) {
    console.error(err)
}

Append Rule

You can also append new rule.

const spl_validator = require('spl_validator')
const validator = new spl_validator()

try {
    let input =  {
        name: 'John Doe'
    };

    validator.AppendRule('Lowercase', async function (value, input, fieldName) {
        if (value === true) {
            input[fieldName] = String(input[fieldName]).toLowerCase()
        }

        return input
    })

    let ruleset = {
        name: { Required: true, Lowercase: true }
    }

    input = await validator.Validate(ruleset, input)

    console.log(input)
} catch (err) {
    console.error(err)
}

Append Rule From File

You can also append a collection of rules in a .js file.

mynewrules.js

const Rules = {}

Rules.Hide = async function (value, input, fieldName) {
    if (value === true) {
        var data = String(input[fieldName])

        input[fieldName] = data.substr(0, Math.ceil(data.length/2)) + String('*').repeat(Math.floor(data.length/2)) 
    }

    return input
}

module.exports = Rules
const spl_validator = require('spl_validator')
const validator = new spl_validator()

try {
    let input =  {
        name: 'John Doe'
    };

    validator.AppendRuleFromFile('mynewrules.js')

    let ruleset = {
        name: { Required: true, Hide: true }
    }

    input = await validator.Validate(ruleset, input)

    console.log(input)
} catch (err) {
    console.error(err)
}
0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

4 years ago

0.1.0

4 years ago