npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dgcode/schema

v0.1.21

Published

generic schema validation utility with yaml integration

Downloads

2

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 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 descriptions (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