# @figedi/typecop

> Json-schema validator

Latest version **1.4.0** (published 2025-01-24) · SEE LICENSE IN LICENSE.md license · 0 weekly downloads

## Install

```sh
npm install @figedi/typecop
pnpm add @figedi/typecop
yarn add @figedi/typecop
bun add @figedi/typecop
```

## Health

**Score 40/100 (D)** — status: stable.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2025-01-24 |
| First published | 2020-07-20 |
| Weekly downloads | 0 |
| License | SEE LICENSE IN LICENSE.md |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=22.11.0 |
| Dependencies | 8 |
| Unpacked size | 36.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | figedi |

## Links

- npm: https://www.npmjs.com/package/@figedi/typecop
- Repository: https://github.com/figedi/typecop
- Issues: https://github.com/figedi/typecop/issues
- npm.io page: https://npm.io/package/@figedi/typecop

## Dependencies (8)

- [ajv](https://npm.io/package/ajv.md) ^8.17.1
- [glob](https://npm.io/package/glob.md) ^11.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [@types/glob](https://npm.io/package/@types/glob.md) ^8.1.0
- [json-schema](https://npm.io/package/json-schema.md) ^0.4.0
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.17.14
- [@types/json-schema](https://npm.io/package/@types/json-schema.md) ^7.0.15
- [@apidevtools/json-schema-ref-parser](https://npm.io/package/@apidevtools/json-schema-ref-parser.md) ^11.7.3

## Recent versions

- 1.4.0 (latest) — 2025-01-24
- 1.3.1 — 2023-07-23
- 1.3.0 — 2023-06-08
- 1.2.2 — 2022-05-18
- 1.2.1 — 2021-03-22
- 1.2.0 — 2021-03-22
- 1.1.0 — 2020-07-20

## README

# Typecop

JSON-schema validation with some extras. It a cop for your types. Sorry for the bad pun.

![timecop](https://upload.wikimedia.org/wikipedia/en/7/79/Timecopposter.jpg "timecop")

## Features
This library is a small wrapper around [ajv](https://github.com/ajv-validator/ajv). It provides some common functionalities I often needed for schema-validation:
- Small file-base schema-repository
- "Better", more readable errors for tracing
- Schema-classes w/ typeguards
- utilities, e.g. for composing schemas for message envelopes

## Usage

Example for using Schema-classes w/ SchemaRepository
```typescript 
import { SchemaRepository } from "@figedi/typecop";

// see resources root.schema.json for definition
type RootExampleType = { 
  id: string;
  column1: ChildExampleType;
  column2: number;
};

const SCHEMA_PATH = join(__dirname, "./resources");
const schemaRepository = SchemaRepository.create(SCHEMA_PATH);
await schemaRepository.preflight(); // loads all schemas, can be run at startup-phase
const RootSchema = new Schema<RootExampleType>(schemaRepository, "root");
RootSchema.is({ some: "object" }) // true/false, typeguard
RootSchema.validate({ some: "object" }) // throws in case of errors
```

Example for base-validator usage
```typescript 
import { createValidator } from "@figedi/typecop";
const validator = createValidator();
// loads the json-schema, de-references all contained $ref's
const compiledSchema = await validator.compile(join(SCHEMA_PATH, "root.schema.json"));
validator.validate(compiledSchema, { some: "object" });
```

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