Skip to content

schemas 0.8429.0

Install from the command line:
Learn more about npm packages
$ npm install @superblocksteam/schemas@0.8429.0
Install via package.json:
"@superblocksteam/schemas": "0.8429.0"

About this version

schemas

When dealing with JSON objects that come from a network request or database, we need to use schema validation. This has some similarities to Typescript type checking, so we have chosen an approach where we generate schemas & validators from Typescript.

Library usage

The Typescript types are converted using https://github.com/vega/ts-json-schema-generator, which has only partial support for Typescript features. So the schemas are using a limited subset of what's possible.

The schema validation is done using ajv, and we use the standalone validators- these are JavaScript functions which get committed to source.

To create a new schema

Create a file in the schemas/src/schemas directory with name of a type. You can use comments to control properties of the schema:

MyType.ts:

export type MyType = {
  name: string;
  // @format uuid
  applicationId: string;
  // @type integer
  count: number;
};

After creating the type, you'll need to run pnpm --filter schemas... build to generate the validator code. You should read the generated JSON Schema to confirm that it matches your rules- this is not always easy.

You need to check in both of the files added to packages/schemas/src/jsonschemas, which will be MyType.ts and MyTypeValidator.js in this case.

To use a schema validator

import {
  validateMyType
} from '@superblocksteam/schemas';

// This will throw an error if it's invalid
const myType = validateMyType(someObject);

Details


Assets

  • schemas-0.8429.0.tgz

Download activity

  • Total downloads 0
  • Last 30 days 0
  • Last week 0
  • Today 0