@hckrnews/validator

Object validator

Usage no npm install needed!

<script type="module">
  import hckrnewsValidator from 'https://cdn.skypack.dev/@hckrnews/validator';
</script>

README

Object validator by hckr.news

NPM version Build Status Coveralls Status Scrutinizer Code Quality

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

Installation

npm install @hckrnews/validator or yarn add @hckrnews/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://hckr.news" }
        { 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://hckr.news" }
            { 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://hckr.news" }
            { 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