JSPM

  • Created
  • Published
  • Downloads 46
  • Score
    100M100P100Q79912F
  • License MIT

A validator format

Package Exports

  • stnl
  • stnl/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stnl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Stnl

A JSON validator format.

import schema, { type InferSchema } from 'stnl';

export const User = schema({
  props: {
    name: {
      type: 'string',
      minLength: 3
    },
    age: {
      type: 'int'
    },
    pwd: {
      type: 'string',
      minLength: 8,
      maxLength: 16
    }
  }
});

export type User = InferSchema<typeof User>;

Compilers

stnl has compilers to convert the schema to other utilities.

Assertion

To JIT compile a schema:

import validateJson from 'stnl/compilers/validate-json';
import type { InferSchema, TSchema } from 'stnl';

// JIT compile the schema
function build<T extends TSchema>(schema: T): (o: any) => o is InferSchema<T> {
  const decls: string[][] = [];
  const content = validateJson(schema, 'o', builder, decls);

  // Same stuff as what '@mapl/compiler' does
  return Function(`'use strict';${decls.map((decl, i) => `var d${i + 1}=${decl.join('')};`).join('')}return (o)=>${content)};`)();
}

const isUser = build(User);
isUser({ name: 'reve', age: 16, pwd: 'revenode' }); // true

To compile a schema without code generation:

import validateJson from 'stnl/compilers/validate-json/compose';

const isUser = validateJson(User);
isUser({ name: 'reve', age: 16, pwd: 'revenode' }); // true

Stringify

To compile a JSON stringifier:

import stringifyJson from 'stnl/compilers/stringify-json';
import type { InferSchema, TSchema } from 'stnl';

// JIT compile the schema
function build<T extends TSchema>(schema: T): (o: InferSchema<T>) => string {
  const decls: string[][] = [];
  const content = stringifyJson(schema, 'o', builder, decls);

  // Same stuff as what '@mapl/compiler' does
  return Function(`'use strict';${decls.map((decl, i) => `var d${i + 1}=${decl.join('')};`).join('')}return (o)=>${content)};`)();
}

const stringifyUser = build(User);
stringifyUser({ name: 'reve', age: 16, pwd: 'revenode' }) === ;