
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
typescript-validator
Advanced tools
A type-safe runtime validator for parsing unsafe data.
This library provides type-checked validator functions that help you take unsafe data (e.g. data from an API request) and validate it against your Typescript interfaces.
When used correctly, Typescript will throw and error if your validator function does not match the type that you are trying to validate.
Install from npm:
npm install --save typescript-validator
Given a type interface:
interface User {
id: number,
name?: string,
email: string,
workspaces: Array<string>
}
You can create a runtime validator function like this:
import * as validate from "typescript-validator"
const validator = validate.object<User>({
id: validate.number(),
name?: validate.optional(validate.string()),
email: validate.string(),
workspaces: validate.array(validate.string())
})
const valid = validator({id: 1, email: "hello", workspaces: []})
// true
The nice thing about this abstraction is that the type-checker will ensure that you've validated all fields of the type interface with the correct validator.
For example, here are some errors the type checker will help you with:
const validator = validate.object<User>({
id: validate.number(),
// name: validate.optional(validate.string()), <-- "Property 'name' is missing in type"
email: validate.string(),
workspaces: validate.array(validate.string())
})
const validator = validate.object<User>({
id: validate.number(),
name: validate.optional(validate.string()),
email: validate.number(), // <-- Type 'string' is not assignable to type 'number'.
workspaces: validate.array(validate.string())
})
You can easily create your own validators as well -- its just a function that returns a boolean. As you can see, we haven't verified that the email string is actually an email or the number of workspaces is not empty. We can easily write these functions ourselves.
const validator = validate.object<User>({
id: validate.number(),
name?: validate.optional(validate.string()),
email: (email: string) => {
return validate.string()(email) && /[^@]+@[^\.]+\.[a-zA-Z]+/.test(value),
workspaces: (value: Array<string>) => {
return validate.array((validate.string()))(value) && value.length > 0
}
})
Happy hacking 🍉
FAQs
Type-safe runtime validator for parsing unsafe data.
The npm package typescript-validator receives a total of 612 weekly downloads. As such, typescript-validator popularity was classified as not popular.
We found that typescript-validator 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
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.