Registry / testing / simple-validators

simple-validators

JSON →
library1.2.0jsnpmunverified

A TypeScript validation library (v1.2.0, actively maintained) for building explicit, customizable schema validators. Unlike libraries like zod or yup, it does not infer schemas from types or use method chaining; instead it provides composable validation functions (validateString, validateBoolean, validateEmail, validateObjectKeys, etc.) that return undefined on failure and collect error/warning messages via options. It is part of the MyST Markdown ecosystem, updated regularly alongside mystmd releases. Key differentiator: no magic, full control over error reporting with user-supplied log functions, and explicit handling of required vs optional keys.

npm install simple-validators
INSTALL
IMPORT
SIG · SIMPLE-VALIDATORS
S
simple-validators
testingjavascriptv1.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

validateString
import { validateString } from 'simple-validators'
import validateString from 'simple-validators'
ESM-only; no default export. Named exports only.
ValidationOptions
import type { ValidationOptions } from 'simple-validators'
import { ValidationOptions } from 'simple-validators'
TypeScript-only type; use `import type` for type-only imports. In CJS (require) it is not available at runtime.
validateObjectKeys
import { validateObjectKeys, validateString, defined, validationError, incrementOptions } from 'simple-validators'
const simple_validators = require('simple-validators'); const validateObjectKeys = simple_validators.validateObjectKeys;
CommonJS require is allowed but not recommended; prefer named ESM imports.
validationError
import { validationError } from 'simple-validators'
import { ValidationError } from 'simple-validators'
Function name is lowercase `validationError`, not the class-like `ValidationError`.

Demonstrates building an Author validator with required id, optional fields, and custom error handling.

import { defined, incrementOptions, validateBoolean, validateEmail, validateObjectKeys, validateString, validationError, ValidationOptions, } from 'simple-validators'; export type Author = { id: string; name?: string; email?: string; corresponding?: boolean; }; export function validateAuthor(input: any, opts: ValidationOptions) { const value = validateObjectKeys( input, { required: ['id'], optional: ['name', 'corresponding', 'email'] }, opts, ); if (value === undefined) return undefined; const id = validateString(value.id, { ...incrementOptions('id', opts), regex: '^[a-z][a-zA-Z0-9]{19}$', }); if (id === undefined) return undefined; const output: Author = { id }; if (defined(value.name)) { output.name = validateString(value.name, incrementOptions('name', opts)); } if (defined(value.email)) { output.email = validateEmail(value.email, incrementOptions('email', opts)); } if (defined(value.corresponding)) { const correspondingOpts = incrementOptions('corresponding', opts); if (value.corresponding && !defined(value.email)) { validationError('corresponding author must have email', correspondingOpts); } output.corresponding = validateBoolean(value.corresponding, correspondingOpts); } return output; }
Debug
Known issues
gotchaAll validation functions return undefined on failure, not a default or partial value. Always check for undefined and handle accordingly.
fix
Use if (value === undefined) return undefined; pattern.
affects: >=1.0.0
deprecatedSince v1.1.0, `validateObjectKeys` requires explicit required and optional arrays; previously a single array was treated as required.
fix
Update calls to { required: [...], optional: [...] }.
affects: >=1.1.0
gotcha`incrementOptions` mutates the `property` key by appending '.' and the new key. Do not reuse opts across sibling properties without resetting.
fix
Always use fresh `incrementOptions` per property; consider cloning opts if needed.
affects: >=1.0.0
deprecatedThe `errorLogFn` and `warningLogFn` in ValidationOptions replace the older `onError` callback (removed in v1.0.0).
fix
Use `errorLogFn` and `warningLogFn` instead of `onError`.
affects: >=1.0.0
gotchaRegex validation in validateString uses `regex` option. Escape patterns properly; invalid regex throws at runtime.
fix
Use new RegExp('<pattern>').test(value) and pass regex as string.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: simple_validators.validateObjectKeys is not a function
Using default import instead of named import.
fix
Change to import { validateObjectKeys } from 'simple-validators'
Uncaught Error: Unable to load author from file
Validation returned undefined; check opts.messages.errors for details.
fix
Inspect opts.messages.errors array to see specific validation failures.
Cannot find module 'simple-validators' or its corresponding type declarations.
TypeScript cannot resolve the module; likely missing dependency.
fix
Run `npm install simple-validators` and ensure tsconfig includes 'node_modules/@types'.
TypeError: Cannot read property 'id' of undefined
validateObjectKeys returned undefined; code accesses value.id without checking.
fix
Add if (value === undefined) return undefined; before accessing properties.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
node:fsoptionalused in example for file reading, but package itself has no dependencies
Agent activity
6 hits · last 30 days
node
6
Resources
simple-validators — npm install simple-validators · libregistry