Skip to content

trojs/validator

This branch is 1 commit ahead of, 142 commits behind master.

Folders and files

NameName
Last commit message
Last commit date
Jun 29, 2024
Jul 25, 2024
Jul 25, 2024
Apr 19, 2024
May 7, 2020
Feb 19, 2020
Jul 25, 2024
Jul 23, 2021
Jul 12, 2022
Jul 24, 2024
Jul 12, 2022
Jul 12, 2022
Feb 19, 2020
Nov 29, 2023
Apr 19, 2024
Apr 19, 2024
Apr 19, 2024
Nov 29, 2023
Dec 18, 2023
Aug 19, 2024
Aug 19, 2024
Jun 29, 2024
Jul 12, 2022

Repository files navigation

Object validator by TroJS

NPM version

Validate the object values by a schema. I hope you like it.

Sponsors 🎉

If it has saved you development time, please consider sponsoring the project with GitHub sponsors!

Or on patreon: https://patreon.com/w3news

Installation

npm install @trojs/validator or yarn add @trojs/validator

Test the package

If you would test the validator, you can just run:

npm install
npm run test

or

yarn
yarn test

Usage

Example schema:

const barSchema = {
    name: "string",
    address: "string",
    drinks: "object",
    "building?": "function|async",
};

Example input:

const barObj = {
    name: 'Jimmys drinks',
    address: 'Somewhere over the rainbow',
    drinks: {
        beer: ['Straffe Hendrik', 'Rochefort', 'St Bernard'],
    },
};

Example usage:

const validator = new Validator(barSchema);

validator.validate(barObj);

Example multi level schema:

const personSchema = {
    name: "string",
    age: "number",
    siblings: "array",
    "?metaData": "object",
    active: "boolean",
    address: {
        street: "string",
        number: "number",
        postalCode: "string",
        city: "string",
        country: "string"
    },
    companies:  {
        name: "string",
        "?website": "string"
    }
};

Example valid data for the person schema:

const personObj = {
    name: "James",
    age: 25,
    siblings: ["Johnnathan"],
    metaData: {},
    active: true,
    address: {
        street: "Streetname",
        number: 1,
        postalCode: "1234AB",
        city: "City",
        country: "Somewehere"
    },
    companies: [
        { name: "Example company 1", website: "https://trojs.org" }
        { name: "Example company 2" }
    ]
}

You can also validate an array of items:

const persons = [
    {
        name: "James",
        age: 25,
        siblings: ["Johnnathan"],
        metaData: {},
        active: true,
        address: {
            street: "Streetname",
            number: 1,
            postalCode: "1234AB",
            city: "City",
            country: "Somewehere"
        },
        companies: [
            { name: "Example company 1", website: "https://trojs.org" }
            { name: "Example company 2" }
        ]
    }
];

validator.validateAll(persons);

And you can also compare to the objects:

const personSchema = {
    name: String,
    age: Number,
    siblings: Array,
    "?metaData": Object,
    active: Boolean,
    address: {
        street: String,
        number: Number,
        postalCode: String,
        city: String,
        country: String
    },
    companies:  {
        name: String,
        "?website": String
    }
};

const persons = [
    {
        name: "James",
        age: 25,
        siblings: ["Johnnathan"],
        metaData: {},
        active: true,
        address: {
            street: "Streetname",
            number: 1,
            postalCode: "1234AB",
            city: "City",
            country: "Somewehere"
        },
        companies: [
            { name: "Example company 1", website: "https://trojs.org" }
            { name: "Example company 2" }
        ]
    }
];

validator.validateAll(persons);

Invalid fields

If there are invalid fields, you can field the fields with .errors. It returns an array with the field name and the expected type.

validator.errors

[
    ['name', String],
    ['age', Number],
    ['siblings', Array],
    ['?metaData', Object],
    ['active', Boolean],
    ['address', addressSchema],
    ['companies', companySchema],
]

Available types:

  • string
  • array
  • object
  • number
  • boolean
  • url
  • date
  • function
  • async

You can check for multiple types. e.g. function|async so it can receive a normal function and also a sync function