# @dgcode/schema

> generic schema validation utility with yaml integration

Latest version **0.1.21** (published 2018-12-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dgcode/schema
pnpm add @dgcode/schema
yarn add @dgcode/schema
bun add @dgcode/schema
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.21 |
| Published | 2018-12-27 |
| First published | 2018-12-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 61.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Aurelien Monasse |
| Maintainers | dgcoder |

## Links

- npm: https://www.npmjs.com/package/@dgcode/schema
- npm.io page: https://npm.io/package/@dgcode/schema

## Dependencies (4)

- [yaml](https://npm.io/package/yaml.md) ^1.1.0
- [word-wrap](https://npm.io/package/word-wrap.md) ^1.2.3
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.1
- [lodash.isplainobject](https://npm.io/package/lodash.isplainobject.md) ^4.0.6

## Recent versions

- 0.1.21 (latest) — 2018-12-27
- 0.1.20 — 2018-12-22
- 0.1.19 — 2018-12-22
- 0.1.18 — 2018-12-22
- 0.1.17 — 2018-12-22
- 0.1.16 — 2018-12-21

## README

# `@dgcode/schema`

generic schema validation utility with yaml integration

# Install

    $ npm install @dgcode/schema

# Usage

    import { Validator } from '@dgcode/schema';

    const validator = new Validator();
    const res = validator.validate(5, {
      type: 'number'
    });

    res.isValid(); // true

# Schema

A _validator_, as showcased above, accepts two arguments while validating: `(value, schema)`. The `value` is the one you want to test, and `schema` defines the expected kind of value to accept.

A _schema_ is an object defining the desired shape of the values you want to validate. They can be as simple as `{ type: 'number' }` or `{ type: 'boolean' }` but can also be customized with more complex properties, such as the following object-matching structure:

    const objectSchema = {
      type: 'object',
      properties: {
        foo: { type: 'string' },
        bar: { type: 'number' }
      }
    };

Here `objectSchema` will match JavaScript objects that implement the expected structure such as `{ foo: 'hello', bar: 5 }`.

# Allowed types

    import { listSchemaTypes } from '@dgcode/schema';

    listSchemaTypes();
    // ['string', 'number', ...] (see below)

You can use the `listSchemaTypes()` method exposed by this library, for convenience, to get an overview of all allowed schema type strings such as `'string'` or `'object'`. Below are the allowed types:

| Type value         | Example                      |
|--------------------|------------------------------|
| `'string'`         | `'foo'`, `''`                |
| `'number'`         | `5`, `10.1`, `-30`           |
| `'boolean'`        | `true`, `false`              |
| `'int'`            | `5`, `0`, `-1`               |
| `'nint'`           | `5`, `100`                   |
| `'bigint'`         | `BigInt(1932839)`, `123n`    |
| `'symbol'`         | `Symbol('hello')`            |
| `'undefined'`      | `undefined`                  |
| `'null'`           | `null`                       |
| `'nullish'`        | `null`, `undefined`          |
| `'truthy'`         | `true`, `1`, `{}`            |
| `'falsy'`          | `false`, `0`, `null`, `''`   |
| `'object'`         | `{}`, `Object.create(null)`  |
| `'object-like`     | `{}`, `function(){}`, `[]`   |
| `'array'`          | `[]`, `Array(5)`             |
| `'function'`       | `function(){}`, `() => {}`   |
| `'any'`, `'*'`     | `null`, `{}`, `5`            |

# YAML string conversion

YAML conversion is facilitated with help of the `toYAML()` method (internally using the [yaml](https://www.npmjs.com/package/yaml) package). It requires a `value` to convert followed by a `schema` describing the expected structure.

While you could just stringify your values by your own means, the main purpose is to inject `description`s (and a little bit of prettyfying) into your YAML output.

    import { toYAML } from '@dgcode/schema';

    const value = {
      foo: {
        bar: 'Hello',
        baz: 5
      }
    };

    const schema = {
      type: 'object',
      properties: {
        foo: {
          type: 'object',
          description: 'An object with detailed properties.',
          properties: {
            bar: {
              type: 'string',
              description: 'Hello,'
            },
            baz: {
              type: 'number',
              description: 'World!'
            }
          }
        }
      }
    };

    const str = toYAML(value, schema);

`str` will be a string with valid YAML output equal to:

```
# An object with detailed properties.
foo:
  # Hello,
  bar: Hello
  # World!
  baz: 5
```

## Prettyfication

If we wanted some additional spacing between comments:

    const str = toYAML(value, schema, {
      spaceAboveComments: true
    });

which will yield:

```
# An object with detailed properties.
foo:

  # Hello,
  bar: Hello

  # World!
  baz: 5 
```

## Inline comments

By default, all comments are rendered above their keys / values. You can opt to render inline comments instead:

    const str = toYAML(value, schema, {
      preferInlineComments: true
    });

which will yield:

```
foo: # An object with detailed properties.
  bar: Hello # Hello,
  baz: 5 # World!
```

You can also pre-configure your schemas instead to have specific components render their descriptions inline by default, with help of the `inlineDescription: true` hint:

    const schema = {
      type: 'object',
      properties: {
        foo: {
          type: 'object',
          description: 'An object with detailed properties.',
          inlineDescription: true,
          properties: {
            bar: {
              type: 'string',
              description: 'Hello,'
            },
            baz: {
              type: 'number',
              description: 'World!'
            }
          }
        }
      }
    };

    const str = toYAML(value, schema);

which will yield:

```
foo: # An object with detailed properties.
  # Hello,
  bar: Hello
  # World!
  baz: 5
```

# Allow multiple value types

**Case 1: simple types**

If your tested value can be, for example, a `string` or a `number`, you can configure your own schema definition with those allowed types as:

    // unknown value, we just know that we allow
    // it to be a string or number
    const value = ...;

    const schema = {
      type: {
        $any: ['string', 'number']
      }
    };

**Case 2: more complex types**

If your tested value can be, for example, a `string` or an `object` with detailed properties, you need to split the schema into two whole schema scenarios:
    
    // unknown value, we just know that we allow
    // it to be a string or an object
    const value = ...;

    const schema = {
      $any: [
        { type: 'string' },
        { type: 'object',
          properties: { ... } }
      ]
    };

**Example**

Note that you can apply both patterns at any nested level of your schema definitions.

# Error detection

> TODO

# License

MIT

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