UNPKG

@quenk/preconditions

Version:
106 lines (105 loc) 3.56 kB
import { Maybe } from '@quenk/noni/lib/data/maybe'; import { Record } from '@quenk/noni/lib/data/record'; import { Node, PrimNode, ArrayNode, ObjectNode, BuiltinsAvailable, ParseContext } from '../parse'; import { JSONPrecondition, PreconditionSpec, Schema } from '..'; /** * BuiltinsConf allow for the configuration of the builtinsAvailable parser * option. * * Specifying "false" disables all builtins, specifying false for a single * schema disables builtins for that schema. */ export type Builtins = boolean | { [K in keyof BuiltinsAvailable]: boolean; } | Partial<BuiltinsAvailable>; /** * BaseOptions for compilation. */ export interface BaseOptions { /** * key indicates the key that custom pipelines will be read from on * a schema. * * Defaults to "preconditions". */ key: string; /** * propMode specifies how to treat the "properties" field of object types. * * Each value corresponds to a precondition from the object module. */ propMode: 'restrict' | 'intersect' | 'disjoin' | 'union'; /** * builtins allows the builtinsAvailable passed to the parser to be * overwritten. */ builtins: Builtins; } /** * CompileContext is a base class used to handle the stages of compiling a * schema. * * The functions provided in this interface are used to shape the precondition * tree created after parsing. */ export declare abstract class CompileContext<T, O extends BaseOptions = BaseOptions> implements ParseContext<T> { options: O; constructor(options: O); /** * identity provides a precondition that always succeeds with its value. */ abstract identity: T; get builtinsAvailable(): Record<true | string[]>; /** * optional wraps a precondition in an optional precondition wrapper to * prevent execution if a value is not specified. */ abstract optional: (prec: T) => T; /** * and joins to preconditions via logical and operation. */ abstract and: (left: T, right: T) => T; /** * or joins to preconditions via a logical or operation. */ abstract or: (left: T, right: T) => T; /** * every joins each precondition in the list into one. */ every: (precs: T[]) => T; /** * properties combines the preconditions for "properties" and * "additionalProperties" section into one. * * The produced precondition must handle record/object type values. The * additionalProperties section is expected to only handled those * properties not explicitly stated in the properties section. */ abstract properties: (props: Record<T>, addProps?: T) => T; /** * items given a precondition, produces a precondition that will apply it * to each element of an array. * * Note: The resulting precondition should ensure the value passed is an * array first. */ abstract items: (prec: T) => T; /** * get a precondition given a spec for it. */ abstract get: (spec: PreconditionSpec<T>) => Maybe<T>; getPipeline: (schema: Schema) => JSONPrecondition[]; /** * visitObject turns an object node into a single precondition. */ visitObject: (node: ObjectNode<T>) => T; /** * visitArray turns an array node into a single precondition. */ visitArray: (node: ArrayNode<T>) => T; /** * visitPrim turns a primitive node into a single precondition. */ visitPrim: ([, precs]: PrimNode<T>) => T; visit: (node: Node<T>) => T; }