
Security News
New React Server Components Vulnerabilities: DoS and Source Code Exposure
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.
validate-all-types
Advanced tools
validate-all-types?This package is a type validator mainly for Typescript (also works with Javascript).
Ever faced those issues where you're trying to make sure a type any is an instance of an interface? This is the issue this package was designed to solve.
With this package, you can safely assert the type for an object and return customised errors if the types are incorrect
$ npm install validate-all-types
or
$ yarn add validate-all-types
then import the module like this:
// Typescript
import { validate } from "validate-all-types"
// Javascript
const { validate } = require("validate-all-types")
Typescript typings are automatically included so there's no need to install anything like @types/validate-all-types !
validate function worksThe validate object we imported earlier is a function which can take 2 (3: optional) parameters.
validate parameters:| Number | Type | Description |
|---|---|---|
1 | any | The object we are checking |
2 | Validator | A specific pattern to compare the object to (more about this later) |
3 | string? | The name of the root object when logs display errors. Defaults to * as root |
The type Validator is something you don't need to worry about.
Just know that it is the Typescript type for a pattern.
Patterns can look like STRING() or NUMBER().
validate returns an object containing:| Property | Type | Description |
|---|---|---|
| success | boolean | Whether the validation of the object was a success or failure. true if success, false if failure |
| errors | string[] | The list of corrections to make if any |
There are infinite combinations of patterns we can make. The complexity of the pattern only depends on how much code you are willing to write.
Here is how to validate a string
console.log(validate("string", STRING()))
// > { success: true, errors: [] }
console.log(validate(0, STRING()))
// > { success: false, errors: [ '*: Expected (*) to be of type `string`' ] }
Notice that in the error message, the root element is called *
because as mentioned earlier, if we don't pass a third parameter into the validate() function, it names the root * by default.
The function STRING with nothing in the parameters represents a type string.
This method can also take in items in the parameters:
| Type | Description |
|---|---|
(empty) | validates if the input is a string |
RegExp | validates if the input is a string and matches the RegExp |
...string[] | validates if the input is a string and matches any of the given strings |
import { validate, STRING } from "validate-all-types"
console.log(validate("string", STRING()))
// > { success: true, errors: [] }
// This returned true because "string" is a string
console.log(validate(0, STRING()))
// > { success: false, errors: [ '*: Expected (*) to be of type `string`' ] }
// This returned false because 0 is not a string
console.log(validate("string", STRING(/^string$/)))
// > { success: true, errors: [] }
// This returned true because "string" matches the RegExp /^string$/
console.log(validate("string", STRING(/^something-else$/)))
// > { success: false, errors: [ '*: Expected (*) to match RegExp (/^something-else$/)' ] }
// This returned false because "string" didn't match the RegExp /^something-else$/
console.log(validate("string", STRING("does", "the", "string", "match", "?")))
// > { success: true, errors: [] }
// This returned true because "string" was passed into STRING() as a parameter
console.log(validate("string", STRING("doesn't", "match"), "my-string"))
// > { success: false, errors: [ `my-string: Expected (my-string) to be in (["doesn't","match"])` ] }
// This returns false because "string" wasn't passed into STRING() as a parameter
// Since I passed a third parameter to the validate function, the root got renamed
NUMBER()Because STRING() validates strings, it's obvious that NUMBER() validates numbers. NUMBER() works the same was as STRING() except allows a different set of parameters:
| Type | Description |
|---|---|
(empty) | validates if the input is a number |
...number[] | validates if the input is a number and matches any of the given numbers |
import { validate, NUMBER } from "validate-all-types"
console.log(validate(3, NUMBER()))
// > { success: true, errors: [] }
console.log(validate("string", NUMBER()))
// > { success: false, errors: [ '*: Expected (*) to be of type `number`' ] }
console.log(validate(3, NUMBER(1, 2, 3, 4, 5)))
// > { success: true, errors: [] }
console.log(validate(3, NUMBER(6, 7, 8, 9, 10)))
// > { success: false, errors: [ '*: Expected (*) to be in ([6,7,8,9,10])' ] }
BOOLEAN()BOOLEAN() allows comparison of booleans only
| Type | Description |
|---|---|
(empty) | validates if the input is a boolean |
boolean | validates if the input is a boolean and if the booleans are equal |
import { validate, BOOLEAN } from "validate-all-types"
console.log(validate(true, BOOLEAN()))
// > { success: true, errors: [] }
console.log(validate("string", BOOLEAN()))
// > { success: false, errors: [ '*: Expected (*) to be of type `boolean`' ] }
console.log(validate(true, BOOLEAN(true)))
// > { success: true, errors: [] }
console.log(validate(false, BOOLEAN(true)))
// > { success: false, errors: [ '*: Expected (*) to be `true`' ] }
NULL()NULL() doesn't allow variations of the parameters
| Type | Description |
|---|---|
(empty) | validates if the input is a null |
import { validate, NULL } from "validate-all-types"
console.log(validate(null, NULL()))
// > { success: true, errors: [] }
console.log(validate(undefined, NULL()))
// > { success: false, errors: [ '*: Expected (*) to be of type `null`' ] }
UNDEFINED()Just like NULL(), UNDEFINED() doesn't allow variations of the parameters
| Type | Description |
|---|---|
| (empty) | validates if the input is a undefined |
import { validate, UNDEFINED } from "validate-all-types"
console.log(validate(undefined, UNDEFINED()))
// > { success: true, errors: [] }
console.log(validate(null, UNDEFINED()))
// > { success: false, errors: [ '*: Expected (*) to be of type `undefined`' ] }
LIST()This one's a bit more complicated. LIST() allows a few sets of parameters:
| Type | Description |
|---|---|
(empty) | validates if the input is a list |
...Validator[] | validates if the input is a list and checks if all items in the list match at least 1 of the Patterns stated |
import { validate, LIST, STRING, NUMBER } from "validate-all-types"
console.log(validate([1, 2, 3, 4, 5], LIST()))
// > { success: true, errors: [] }
console.log(validate({ property: "value" }, LIST()))
// > { success: false, errors: [ '*: Expected (*) to be of type `array`' ] }
console.log(validate(["one", "two", "three"], LIST(STRING())))
// > { success: true, errors: [] }
console.log(validate([1, "two", 3], LIST(NUMBER())))
// > { success: false, errors: [ '* > [1]: Expected [1] to be of pattern defined' ] }
console.log(validate([1, "two", []], LIST(STRING(), NUMBER(), LIST())))
// > { success: true, errors: [] }
// And yes we also can verify LIST() within LIST()
console.log(validate([1, "two", null], LIST(STRING(), NUMBER())))
// > { success: false, errors: [ '* > [2]: Expected [2] to be of pattern defined' ] }
const usernames = ["jack", "_jack", "-jack"]
console.log(validate(usernames, LIST(STRING(/^[a-zA-Z]/))))
// > {
// > success: false,
// > errors: [
// > '* > [1]: Expected [1] to be of pattern defined',
// > '* > [2]: Expected [2] to be of pattern defined'
// > ]
// > }
// Not every username matched /^[a-zA-Z]/
const codes = [34, 76, 92]
console.log(validate(codes, LIST(NUMBER(34, 76, 92))))
// > { success: true, errors: [] }
// Every code matched items in [34, 76, 92]
This way, we can make checking of list types much more detailed
OBJECT()We can use OBJECT() to validate objects.
OBJECT() only allows 1 optional parameter which maps out what the inner properties will look like
import { validate, OBJECT } from "validate-all-types"
console.log(validate({ property: "value" }, OBJECT()))
// > { success: true, errors: [] }
// Since { property: "value" } is an object, validate() returned true
console.log(validate({ property: "value" }, OBJECT({})))
// > {
// > success: false,
// > errors: [ '* > name: No type definitions for (property)' ]
// > }
// The pattern of {} means the object must have no properties
console.log(validate(
{ property: "value" },
OBJECT({ property: STRING() }
)))
// > { success: true, errors: [] }
// We set the OBJECT's params to an object with a property "property" and a value "value"
// Since "value" matches STRING(), validate() returned true
console.log(validate({
property: "value"
}, OBJECT({
prop: STRING()
})
))
// > {
// > success: false,
// > errors: [
// > '*: Expected (*) to contain property (prop)',
// > '* > property: No type definitions for (property)'
// > ]
// > }
// Since there is no property for the type validation "prop", we got an error
// Since there is no type validation for the property "property", we got an error
console.log(validate(
{
property: "value",
layer: {
deepProperty: ["", 0, null, undefined, false]
}
},
OBJECT({
property: STRING(),
layer: OBJECT({
deepProperty: LIST(STRING(), NUMBER(0), NULL(), UNDEFINED(), BOOLEAN(false))
})
})
))
// > { success: true, errors: [] }
// We can even nest OBJECT() in OBJECT()
OR()If you want either of a few patterns to match, use the OR() operator.
This function takes multiple parameters:
| Type | Description |
|---|---|
...Validator[] | A list of patterns to test on the input |
import { validate, OR, STRING, NUMBER, BOOLEAN } from "validate-all-types"
console.log(validate("string", OR()))
// > Error: *: Expected developer to provide at least 1 pattern for the OR operation
// An OR operation only works with at least one input
console.log(validate(
"string",
OR(STRING(), NUMBER())
))
// > { success: true, errors: [] }
console.log(validate(
"string",
OR(BOOLEAN(), NUMBER())
))
// > {
// > success: false,
// > errors: [ '*: Expected (*) to match at least one of the given patterns' ]
// > }
validate_express with Express.jsYou can also import the module as a middleware to be used with express.
This way, you can verify the types of the req.body or req.params
before invalid types mess your code up
import { validate_express, OBJECT, STRING } from "validate-all-types"
// ----snip----
app.post("/body",
validate_express("body", OBJECT({ usnm: STRING(), pswd: STRING() })),
(req, res) => {
const { usnm, pswd } = req.body as { usnm: string, pswd: string }
console.log(`Username: ${usnm}`, `Password: ${pswd}`)
res.end()
}
)
// ----snip----
The validate_express takes in 2 parameters:
| Number | Type | Description |
|---|---|---|
1 | "body" | "pattern" | Can either verify the req.body or req.params object |
2 | Validator | Pattern to compare the object with |
Because of the middleware, in Typescript you can now safely use type assertions. Also, now for both Typescript and Javascript, you can safely use the variables like they are the defined types and not have to worry about invalid types crashing your server!
FAQs
Validator for all interfaces
The npm package validate-all-types receives a total of 0 weekly downloads. As such, validate-all-types popularity was classified as not popular.
We found that validate-all-types demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.