# @trojs/validator

> Object validator

Latest version **12.0.4** (published 2026-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @trojs/validator
pnpm add @trojs/validator
yarn add @trojs/validator
bun add @trojs/validator
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 12.0.4 |
| Published | 2026-02-06 |
| First published | 2024-04-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 20 |
| Dependencies | 0 |
| Unpacked size | 14.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Pieter Wigboldus |
| Maintainers | epeus, w3news |
| Keywords | objects, validator |

## Links

- npm: https://www.npmjs.com/package/@trojs/validator
- Repository: https://github.com/trojs/validator
- Homepage: https://github.com/trojs/validator#readme
- Issues: https://github.com/trojs/validator/issues
- Funding: https://github.com/sponsors/w3nl
- npm.io page: https://npm.io/package/@trojs/validator

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 12.0.4 (latest) — 2026-02-06
- 12.0.3 — 2026-01-22
- 12.0.2 — 2026-01-09
- 12.0.1 — 2025-12-08
- 12.0.0 — 2025-10-31
- 11.1.5 — 2025-09-07
- 11.1.4 — 2025-09-07
- 11.1.3 — 2025-09-05
- 11.1.2 — 2025-07-30
- 11.1.1 — 2025-07-30
- 11.1.0 — 2025-07-30
- 11.0.6 — 2025-06-20
- 11.0.5 — 2025-06-13
- 11.0.4 — 2025-05-27
- 11.0.2 — 2025-03-03
- … 11 more at https://npm.io/package/@trojs/validator/versions

## README

# Object validator by TroJS

[![NPM version][npm-image]][npm-url]

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](https://github.com/sponsors/w3nl)
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:
```javascript
const barSchema = {
    name: "string",
    address: "string",
    drinks: "object",
    "building?": "function|async",
};
```

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

Example usage:
```javascript
const validator = new Validator(barSchema);

validator.validate(barObj);
```

Example multi level schema:
```javascript
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:
```javascript
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:
```javascript
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:
```javascript
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.

```javascript
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

[npm-url]: https://www.npmjs.com/package/@trojs/validator
[npm-image]: https://img.shields.io/npm/v/@trojs/validator.svg

---
_Source: https://npm.io/package/@trojs/validator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
