9.0.4 • Published 26 days ago

@hckrnews/validator v9.0.4

Weekly downloads
37
License
MIT
Repository
github
Last release
26 days ago

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.

Sponsors :tada:

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

But me a coffee/book: https://www.buymeacoffee.com/hckrnews Or on patreon: https://patreon.com/w3news

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

9.0.4

26 days ago

9.0.3

2 months ago

9.0.2

3 months ago

9.0.1

4 months ago

8.0.13

5 months ago

9.0.0

5 months ago

8.0.10

6 months ago

8.0.12

5 months ago

8.0.11

5 months ago

8.0.9

6 months ago

8.0.8

7 months ago

8.0.7

10 months ago

8.0.6

10 months ago

8.0.5

1 year ago

8.0.4

1 year ago

8.0.1

1 year ago

8.0.0

1 year ago

8.0.3

1 year ago

8.0.2

1 year ago

6.0.0

2 years ago

7.0.0

2 years ago

5.0.4

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.4.1

3 years ago

4.4.0

3 years ago

4.4.2

3 years ago

4.3.0

3 years ago

4.2.5

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.1.0

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.0.0

3 years ago

2.1.7

3 years ago

2.1.6

3 years ago

2.1.3

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago