Skip to content

aquapi/stnl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stnl

A 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.

Example usage:

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

function buildValidator<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 = buildValidator(User);
isUser({ name: 'reve', age: 16, pwd: 'revenode' }); // true

About

A validator format

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published