@quenk/preconditions
Version:
Make data satisfy constraints before using.
82 lines (81 loc) • 4.04 kB
TypeScript
import { Type } from '@quenk/noni/lib/data/type';
import { Record } from '@quenk/noni/lib/data/record';
import { Value, Object } from '@quenk/noni/lib/data/jsonx';
import { Result } from './result';
import { Precondition, Preconditions } from './';
/**
* isRecord tests if the value is an js object (and not an Array).
*/
export declare const isRecord: <A>(value: Type) => Result<any, Record<A>>;
/**
* restrict applies a record of preconditions to an input object keeping
* only those properties that have a matching precondition.
*
* If any of the preconditions fail, the whole object is considered a failure.
*/
export declare const restrict: <A, B, R extends Record<B>>(tests: Preconditions<A, B>) => Precondition<Record<A>, R>;
/**
* disjoint applies a record of preconditions to a javascript object
* producing a new object with the final value of each precondition
* and the values of any additional properties in the input object.
*
* If any of the preconditions fail, the whole object is considered a failure.
*/
export declare const disjoint: <A, B, R extends Record<B>>(tests: Preconditions<A, B>) => Precondition<Record<A>, R>;
/**
* intersect applies only the properties in a record of preconditions
* that exist in the target input object. The resulting value is an
* object with properties that exist in the input object that have had a
* matching precondition applied.
*
* If any of the preconditions fail, the whole object is considered a failure.
*/
export declare const intersect: <A, B, R extends Record<B>>(tests: Preconditions<A, B>) => Precondition<Record<A>, R>;
/**
* union applies a record of preconditions to an input object.
*
* Union results in an object that has both the results of applied preconditions for
* found properties and any additional properties on the input object.
*
* If any of the preconditions fail, the whole object is considered a failure.
*/
export declare const union: <A, B, R extends Record<B>>(tests: Preconditions<A, B>) => Precondition<Record<A>, R>;
/**
* map applies the same Precondition to each property of an object.
*
* If any of the preconditions fail, the whole object is considered a failure.
*/
export declare const map: <A, B>(prec: Precondition<A, B>) => Precondition<Record<A>, Record<B>>;
/**
* merge the properties of the value into the provided object.
*
* Any conflicting properties resolve to the value's property.
*/
export declare const merge: <A extends object, B extends object>(target: B) => Precondition<A, A & B>;
/**
* mergeRight is like merge except conflicts are resolved with the target's
* property.
*/
export declare const mergeRight: <A extends object, B extends object>(target: B) => Precondition<A, A & B>;
/**
* exclude the specified keys from a record value.
*/
export declare const exclude: <A, R extends Record<A>>(keys: string | string[]) => Precondition<R, R>;
/**
* schemaProperties is a special precondition used internally for precondition
* generation from object type schema.
*
* @param props - The preconditions parsed from the property section of
* the object schema. If there are no properties, the
* precondition generated will drop any property values
* unless the "additionalProperties" section is specified.
*
* @param propsPrec - A precondition to wrap the props record in. This
* should be one of "restrict", "intersect", "union" etc.
*
* @param addPropsPrec - A precondition parsed from the "additionalProperties"
* section. This is used to intercept any not explicitly
* declared properties. Properties declared in the
* properties section are not passed to this precondition.
*/
export declare const schemaProperties: (propsWrap: (props: Preconditions<Value, Value>) => Precondition<Object, Object>, props: Preconditions<Value, Value>, addPropsPrec?: Precondition<Value, Value>) => Precondition<Value, Value>;