Comparing version 5.1.0 to 5.1.1
@@ -1,15 +0,2 @@ | ||
import { Blueprint, DeepPartial, SchemaValidateOptions } from './types'; | ||
export interface OptimalOptions { | ||
/** Include a filename in validation error messages. Can be used in conjunction with | ||
`name`. */ | ||
file?: string; | ||
/** Include a unique identifier in validation error messages. Can be used in conjunction | ||
with `file`. */ | ||
name?: string; | ||
/** @internal */ | ||
prefix?: string; | ||
/** Allow unknown fields to be passed within the object being validated. Otherwise, an error will | ||
be thrown. */ | ||
unknown?: boolean; | ||
} | ||
import { Blueprint, DeepPartial, OptimalOptions, SchemaValidateOptions } from './types'; | ||
export interface Optimal<T extends object> { | ||
@@ -16,0 +3,0 @@ /** Modify optimal options after instantiation. */ |
@@ -0,1 +1,2 @@ | ||
import { OptimalOptions } from './types'; | ||
import { ValidationError } from './ValidationError'; | ||
@@ -7,4 +8,4 @@ export declare class OptimalError extends ValidationError { | ||
schema: string; | ||
constructor(errors: Error[]); | ||
constructor(errors: Error[], options: OptimalOptions); | ||
} | ||
//# sourceMappingURL=OptimalError.d.ts.map |
@@ -23,3 +23,3 @@ import { CommonCriterias, Constructor, InferNullable, InstanceOfOptions, NotNull, NotUndefined, Options, Schema } from '../types'; | ||
*/ | ||
export declare function instance(): InstanceSchema<Object | null>; | ||
export declare function instance<T = Object>(): InstanceSchema<T | null>; | ||
//# sourceMappingURL=instance.d.ts.map |
@@ -167,2 +167,15 @@ export declare type Primitive = bigint | boolean | number | string | symbol | null | undefined; | ||
export declare type Predicate<T> = (value: T | null | undefined) => boolean; | ||
export interface OptimalOptions { | ||
/** Include a filename in validation error messages. Can be used in conjunction with | ||
`name`. */ | ||
file?: string; | ||
/** Include a unique identifier in validation error messages. Can be used in conjunction | ||
with `file`. */ | ||
name?: string; | ||
/** @internal */ | ||
prefix?: string; | ||
/** Allow unknown fields to be passed within the object being validated. Otherwise, an error will | ||
be thrown. */ | ||
unknown?: boolean; | ||
} | ||
export declare type InferFromObject<T> = { | ||
@@ -169,0 +182,0 @@ [K in keyof T]: Infer<T[K]>; |
@@ -521,3 +521,3 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
/** Unique name for this validation. */ | ||
constructor(errors) { | ||
constructor(errors, options) { | ||
super('The following validations have failed:'); | ||
@@ -530,2 +530,18 @@ | ||
this.name = 'OptimalError'; | ||
let label = ''; | ||
if (options.name) { | ||
this.schema = options.name; | ||
label = `\`${options.name}\``; | ||
} | ||
if (options.file) { | ||
this.file = options.file; | ||
label = label ? `${label} (${options.file})` : `\`${options.file}\``; | ||
} | ||
if (label) { | ||
this.message = `The following validations have failed for ${label}:`; | ||
} | ||
this.addErrors(errors); | ||
@@ -1975,20 +1991,3 @@ } | ||
} catch (error) { | ||
const invalid = error instanceof OptimalError ? error : new OptimalError([error]); | ||
let prefix = ''; | ||
if (options.name) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
if (options.file) { | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
throw new OptimalError([error], options); | ||
} | ||
@@ -1995,0 +1994,0 @@ } |
@@ -526,3 +526,3 @@ // Bundled with Packemon: https://packemon.dev | ||
/** Unique name for this validation. */ | ||
constructor(errors) { | ||
constructor(errors, options) { | ||
super('The following validations have failed:'); | ||
@@ -535,2 +535,18 @@ | ||
this.name = 'OptimalError'; | ||
let label = ''; | ||
if (options.name) { | ||
this.schema = options.name; | ||
label = `\`${options.name}\``; | ||
} | ||
if (options.file) { | ||
this.file = options.file; | ||
label = label ? `${label} (${options.file})` : `\`${options.file}\``; | ||
} | ||
if (label) { | ||
this.message = `The following validations have failed for ${label}:`; | ||
} | ||
this.addErrors(errors); | ||
@@ -1980,20 +1996,3 @@ } | ||
} catch (error) { | ||
const invalid = error instanceof OptimalError ? error : new OptimalError([error]); | ||
let prefix = ''; | ||
if (options.name) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
if (options.file) { | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
throw new OptimalError([error], options); | ||
} | ||
@@ -2000,0 +1999,0 @@ } |
@@ -527,3 +527,3 @@ // Bundled with Packemon: https://packemon.dev | ||
/** Unique name for this validation. */ | ||
constructor(errors) { | ||
constructor(errors, options) { | ||
super('The following validations have failed:'); | ||
@@ -536,2 +536,18 @@ | ||
this.name = 'OptimalError'; | ||
let label = ''; | ||
if (options.name) { | ||
this.schema = options.name; | ||
label = `\`${options.name}\``; | ||
} | ||
if (options.file) { | ||
this.file = options.file; | ||
label = label ? `${label} (${options.file})` : `\`${options.file}\``; | ||
} | ||
if (label) { | ||
this.message = `The following validations have failed for ${label}:`; | ||
} | ||
this.addErrors(errors); | ||
@@ -1951,20 +1967,3 @@ } | ||
} catch (error) { | ||
const invalid = error instanceof OptimalError ? error : new OptimalError([error]); | ||
let prefix = ''; | ||
if (options.name) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
if (options.file) { | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
throw new OptimalError([error], options); | ||
} | ||
@@ -1971,0 +1970,0 @@ } |
{ | ||
"name": "optimal", | ||
"version": "5.1.0", | ||
"version": "5.1.1", | ||
"description": "Build, validate, and transform values with immutable typed schemas.", | ||
@@ -56,3 +56,3 @@ "main": "./lib/node/index.js", | ||
}, | ||
"gitHead": "f25b4ebe01b43495907a30ef62add64fc5724d1a" | ||
"gitHead": "265258e5e01c77c33b87637ade0743f0e09b8d19" | ||
} |
import { isObject } from './helpers'; | ||
import { OptimalError } from './OptimalError'; | ||
import { shape } from './schemas/shape'; | ||
import { Blueprint, DeepPartial, SchemaValidateOptions, UnknownObject } from './types'; | ||
import { | ||
Blueprint, | ||
DeepPartial, | ||
OptimalOptions, | ||
SchemaValidateOptions, | ||
UnknownObject, | ||
} from './types'; | ||
import { ValidationError } from './ValidationError'; | ||
export interface OptimalOptions { | ||
/** Include a filename in validation error messages. Can be used in conjunction with | ||
`name`. */ | ||
file?: string; | ||
/** Include a unique identifier in validation error messages. Can be used in conjunction | ||
with `file`. */ | ||
name?: string; | ||
/** @internal */ | ||
prefix?: string; | ||
/** Allow unknown fields to be passed within the object being validated. Otherwise, an error will | ||
be thrown. */ | ||
unknown?: boolean; | ||
} | ||
export interface Optimal<T extends object> { | ||
@@ -61,21 +53,3 @@ /** Modify optimal options after instantiation. */ | ||
} catch (error: unknown) { | ||
const invalid = | ||
error instanceof OptimalError ? error : new OptimalError([error as ValidationError]); | ||
let prefix = ''; | ||
if (options.name) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
if (options.file) { | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
throw new OptimalError([error as ValidationError], options); | ||
} | ||
@@ -82,0 +56,0 @@ }, |
@@ -0,1 +1,2 @@ | ||
import { OptimalOptions } from './types'; | ||
import { ValidationError } from './ValidationError'; | ||
@@ -10,8 +11,25 @@ | ||
constructor(errors: Error[]) { | ||
constructor(errors: Error[], options: OptimalOptions) { | ||
super('The following validations have failed:'); | ||
this.name = 'OptimalError'; | ||
let label = ''; | ||
if (options.name) { | ||
this.schema = options.name; | ||
label = `\`${options.name}\``; | ||
} | ||
if (options.file) { | ||
this.file = options.file; | ||
label = label ? `${label} (${options.file})` : `\`${options.file}\``; | ||
} | ||
if (label) { | ||
this.message = `The following validations have failed for ${label}:`; | ||
} | ||
this.addErrors(errors); | ||
} | ||
} |
@@ -37,4 +37,4 @@ import { createSchema } from '../createSchema'; | ||
*/ | ||
export function instance() { | ||
return createSchema<InstanceSchema<Object | null>>( | ||
export function instance<T = Object>() { | ||
return createSchema<InstanceSchema<T | null>>( | ||
{ | ||
@@ -41,0 +41,0 @@ api: { ...commonCriteria, ...classCriteria }, |
@@ -235,2 +235,18 @@ export type Primitive = bigint | boolean | number | string | symbol | null | undefined; | ||
// OPTIMAL | ||
export interface OptimalOptions { | ||
/** Include a filename in validation error messages. Can be used in conjunction with | ||
`name`. */ | ||
file?: string; | ||
/** Include a unique identifier in validation error messages. Can be used in conjunction | ||
with `file`. */ | ||
name?: string; | ||
/** @internal */ | ||
prefix?: string; | ||
/** Allow unknown fields to be passed within the object being validated. Otherwise, an error will | ||
be thrown. */ | ||
unknown?: boolean; | ||
} | ||
// INFER | ||
@@ -237,0 +253,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
8218
430829