# simple-validators

> Schema validation functions and error reporting framework

Latest version **1.2.1** (published 2026-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-validators
pnpm add simple-validators
yarn add simple-validators
bun add simple-validators
```

## Health

**Score 75/100 (B)** — status: active.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2026-10-26 |
| First published | 2022-09-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 29.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 533 |
| Author | Franklin Koch |
| Maintainers | rowanc1, ebp-bot |

## Links

- npm: https://www.npmjs.com/package/simple-validators
- Repository: https://github.com/jupyter-book/mystmd
- Homepage: https://github.com/jupyter-book/mystmd/tree/main/packages/simple-validators
- Issues: https://github.com/jupyter-book/mystmd/issues
- npm.io page: https://npm.io/package/simple-validators

## Recent versions

- 1.2.1 (latest) — 2026-10-26
- 1.2.0 — 2026-01-21
- 1.1.1 — 2025-06-16
- 1.1.0 — 2024-08-22
- 1.0.6 — 2024-07-24
- 1.0.5 — 2024-05-28
- 1.0.4 — 2023-11-23
- 1.0.3 — 2023-11-09
- 1.0.2 — 2023-10-16
- 1.0.1 — 2023-07-03
- 1.0.0 — 2023-06-09
- 0.0.3 — 2023-01-04
- 0.0.2 — 2022-11-14
- 0.0.1 — 2022-09-16

## README

# simple-validators

Schema validation functions and error reporting framework

## Scope

This package provides basic validation functions for building more complex schema validation. It is explicit and verbose with no magic to infer anything from existing types. Rather than throwing validation errors, these functions pass error/warning messages out as options and return `undefined`; the consumer may then choose how to handle error reporting.

## Usage

```typescript
import fs from 'node:fs';
import {
  defined,
  incrementOptions,
  validateBoolean,
  validateEmail,
  validateObjectKeys,
  validateString,
  validationError,
  ValidationOptions,
} from 'simple-validators';

// Define typescript type
export type Author = {
  id: string;
  name?: string;
  email?: string;
  corresponding?: boolean;
};

// Define validation function for object, which explicitly checks each property
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;
}

// Consume validation function with logging and error handling
export function loadAuthorFromFile(authorFile: string) {
  const rawAuthor = JSON.parse(fs.readFileSync(authorFile).toString());
  const opts: ValidationOptions = {
    file: authorFile,
    property: 'author',
    messages: {},
    errorLogFn: (message: string) => console.log(`Error: ${message}`),
    warningLogFn: (message: string) => console.log(`Warning: ${message}`),
  };
  const author = validateAuthor(rawAuthor, opts);
  if (!author || opts.messages.errors?.length) {
    throw new Error(`Unable to load author from file ${authorFile}`);
  }
  return author;
}
```

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