pure-parse
Version:
Strongly typed validation library that decouples type aliases from validation logic
50 lines (49 loc) • 2.49 kB
TypeScript
import { Guard, OptionalGuard } from './Guard';
import { OptionalKeys, WithOptionalFields } from '../internals/utility-types';
/**
* Objects have a fixed set of properties of different types.
* @example
* ```ts
* Object with both required and optional properties:
* const isUser = objectGuard({
* id: isNumber,
* uid: isString,
* active: isBoolean,
* email: optional(isString),
* })
* ```
* Note that optional properties will be inferred as required properties that can be assigned `undefined`. See {@link Infer} > limitations for in-depth information.
* @example
* Annotate explicitly:
* ```ts
* type User = {
* id: number
* name: string
* }
*
* const isUser = object<User>({
* id: isNumber,
* name: isString,
* })
* ```
* @see {@link objectGuardCompiled} for just-in-time compiled version of this function.
* @param schema maps keys to validation functions.
*/
export declare const objectGuard: <T extends Record<string, unknown>>(schema: { [K in keyof T]-?: {} extends Pick<T, K> ? OptionalGuard<T[K]> : Guard<T[K]>; }) => Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>;
/**
* Same as {@link objectGuard}, but performs just-in-time (JIT) compilation with the `Function` constructor, which greatly increases the execution speed of the validation.
* However, the JIT compilation is slow and gets executed at the time when the validation function is constructed.
* When using this function at the module level, it is recommended to wrap it in {@link lazy} to defer the JIT compilation to when the validation function is called for the first time.
* This function will be blocked in environments where the `Function` constructor is blocked; for example, when the [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) policy is set without the `'unsafe-eval`' directive.
* @example
* Defer the JIT compilation to when the validation function is called for the first time.
* ```ts
* const isUser = lazy(() => objectGuardCompiled({
* id: isNumber,
* name: isString,
* })
* ```
* @see {@link objectGuard} for a non-just-in-time compiled version of this function.
* @param schema maps keys to validation functions.
*/
export declare const objectGuardCompiled: <T extends Record<string, unknown>>(schema: { [K in keyof T]-?: {} extends Pick<T, K> ? OptionalGuard<T[K]> : Guard<T[K]>; }) => Guard<OptionalKeys<T> extends undefined ? T : WithOptionalFields<T>>;