Comparing version 5.0.0-alpha.3 to 5.0.0-alpha.4
@@ -21,3 +21,4 @@ import { Constructor, Schema, UnknownObject } from './types'; | ||
export declare function pathKey(path: string): string; | ||
export declare function prettyValue(value: unknown): string | null; | ||
export declare function tryAndCollect(validator: () => boolean | void, validError: ValidationError, collectErrors?: boolean): boolean; | ||
//# sourceMappingURL=helpers.d.ts.map |
@@ -21,3 +21,6 @@ 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; } | ||
const key = pathKey(path); | ||
this.message = `${key.includes('[') ? `Invalid member "${key}".` : `Invalid field "${key}".`} ${this.message}`; | ||
const valueLabel = prettyValue(value); | ||
const typeLabel = key.includes('[') ? 'member' : 'field'; | ||
const label = valueLabel ? `Invalid ${typeLabel} "${key}" with value ${valueLabel}.` : `Invalid ${typeLabel} "${key}".`; | ||
this.message = `${label} ${this.message}`; | ||
} | ||
@@ -153,2 +156,29 @@ } | ||
function prettyValue(value) { | ||
switch (typeof value) { | ||
case 'string': | ||
return `"${value}"`; | ||
case 'number': | ||
case 'function': | ||
return String(value); | ||
case 'object': | ||
{ | ||
if (value === null) { | ||
return `\`null\``; | ||
} | ||
if (value.constructor !== Object) { | ||
return value.constructor.name === 'Array' ? null : `\`${value.constructor.name}\``; | ||
} | ||
return null; | ||
} | ||
default: | ||
return `\`${value}\``; | ||
} | ||
} | ||
function tryAndCollect(validator, validError, collectErrors) { | ||
@@ -213,3 +243,3 @@ let result = false; | ||
value = typeof defaultValue === 'function' ? defaultValue(path, currentObject, rootObject) : defaultValue; | ||
invalid(!state.required, 'Field is required and must be defined.', path); | ||
invalid(!state.required, 'Field is required and must be defined.', path, undefined); | ||
} else { | ||
@@ -226,3 +256,3 @@ if (process.env.NODE_ENV !== "production" && metadata.deprecatedMessage) { | ||
if (value === null) { | ||
invalid(state.nullable, 'Null is not allowed.', path); | ||
invalid(state.nullable, 'Null is not allowed.', path, null); | ||
} // Run validations and produce a new value | ||
@@ -233,3 +263,3 @@ | ||
validators.forEach(test => { | ||
if (test.skipIfNull && value === null || test.skipIfOptional && !state.required && value === state.defaultValue) { | ||
if (test.skipIfNull && value === null || test.skipIfOptional && !state.required && value === state.defaultValue || state.never) { | ||
return; | ||
@@ -1022,3 +1052,3 @@ } | ||
if (process.env.NODE_ENV !== "production") { | ||
invariant(isObject(schemas) && Object.keys(schemas).length > 0 && Object.values(schemas).every(isSchema), 'A non-empty object of schemas are required for a shape.'); | ||
invariant(isObject(schemas) && Object.values(schemas).every(isSchema), 'An object of schemas are required for a blueprint.'); | ||
} | ||
@@ -1696,2 +1726,3 @@ | ||
let invalid; | ||
let prefix = ''; | ||
@@ -1707,2 +1738,3 @@ if (error instanceof OptimalError) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
@@ -1712,4 +1744,9 @@ | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
@@ -1716,0 +1753,0 @@ } |
@@ -27,3 +27,6 @@ // Bundled with Packemon: https://packemon.dev | ||
const key = pathKey(path); | ||
this.message = `${key.includes('[') ? `Invalid member "${key}".` : `Invalid field "${key}".`} ${this.message}`; | ||
const valueLabel = prettyValue(value); | ||
const typeLabel = key.includes('[') ? 'member' : 'field'; | ||
const label = valueLabel ? `Invalid ${typeLabel} "${key}" with value ${valueLabel}.` : `Invalid ${typeLabel} "${key}".`; | ||
this.message = `${label} ${this.message}`; | ||
} | ||
@@ -159,2 +162,29 @@ } | ||
function prettyValue(value) { | ||
switch (typeof value) { | ||
case 'string': | ||
return `"${value}"`; | ||
case 'number': | ||
case 'function': | ||
return String(value); | ||
case 'object': | ||
{ | ||
if (value === null) { | ||
return `\`null\``; | ||
} | ||
if (value.constructor !== Object) { | ||
return value.constructor.name === 'Array' ? null : `\`${value.constructor.name}\``; | ||
} | ||
return null; | ||
} | ||
default: | ||
return `\`${value}\``; | ||
} | ||
} | ||
function tryAndCollect(validator, validError, collectErrors) { | ||
@@ -219,3 +249,3 @@ let result = false; | ||
value = typeof defaultValue === 'function' ? defaultValue(path, currentObject, rootObject) : defaultValue; | ||
invalid(!state.required, 'Field is required and must be defined.', path); | ||
invalid(!state.required, 'Field is required and must be defined.', path, undefined); | ||
} else { | ||
@@ -232,3 +262,3 @@ if (process.env.NODE_ENV !== "production" && metadata.deprecatedMessage) { | ||
if (value === null) { | ||
invalid(state.nullable, 'Null is not allowed.', path); | ||
invalid(state.nullable, 'Null is not allowed.', path, null); | ||
} // Run validations and produce a new value | ||
@@ -239,3 +269,3 @@ | ||
validators.forEach(test => { | ||
if (test.skipIfNull && value === null || test.skipIfOptional && !state.required && value === state.defaultValue) { | ||
if (test.skipIfNull && value === null || test.skipIfOptional && !state.required && value === state.defaultValue || state.never) { | ||
return; | ||
@@ -1070,3 +1100,3 @@ } | ||
if (process.env.NODE_ENV !== "production") { | ||
invariant(isObject(schemas) && Object.keys(schemas).length > 0 && Object.values(schemas).every(isSchema), 'A non-empty object of schemas are required for a shape.'); | ||
invariant(isObject(schemas) && Object.values(schemas).every(isSchema), 'An object of schemas are required for a blueprint.'); | ||
} | ||
@@ -1760,2 +1790,3 @@ | ||
let invalid; | ||
let prefix = ''; | ||
@@ -1771,2 +1802,3 @@ if (error instanceof OptimalError) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
@@ -1776,4 +1808,9 @@ | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
@@ -1780,0 +1817,0 @@ } |
@@ -31,3 +31,6 @@ // Bundled with Packemon: https://packemon.dev | ||
const key = pathKey(path); | ||
this.message = `${key.includes('[') ? `Invalid member "${key}".` : `Invalid field "${key}".`} ${this.message}`; | ||
const valueLabel = prettyValue(value); | ||
const typeLabel = key.includes('[') ? 'member' : 'field'; | ||
const label = valueLabel ? `Invalid ${typeLabel} "${key}" with value ${valueLabel}.` : `Invalid ${typeLabel} "${key}".`; | ||
this.message = `${label} ${this.message}`; | ||
} | ||
@@ -162,2 +165,29 @@ } | ||
function prettyValue(value) { | ||
switch (typeof value) { | ||
case 'string': | ||
return `"${value}"`; | ||
case 'number': | ||
case 'function': | ||
return String(value); | ||
case 'object': | ||
{ | ||
if (value === null) { | ||
return `\`null\``; | ||
} | ||
if (value.constructor !== Object) { | ||
return value.constructor.name === 'Array' ? null : `\`${value.constructor.name}\``; | ||
} | ||
return null; | ||
} | ||
default: | ||
return `\`${value}\``; | ||
} | ||
} | ||
function tryAndCollect(validator, validError, collectErrors) { | ||
@@ -220,3 +250,3 @@ let result = false; | ||
value = typeof defaultValue === 'function' ? defaultValue(path, currentObject, rootObject) : defaultValue; | ||
invalid(!state.required, 'Field is required and must be defined.', path); | ||
invalid(!state.required, 'Field is required and must be defined.', path, undefined); | ||
} else { | ||
@@ -233,3 +263,3 @@ if (process.env.NODE_ENV !== "production" && metadata.deprecatedMessage) { | ||
if (value === null) { | ||
invalid(state.nullable, 'Null is not allowed.', path); | ||
invalid(state.nullable, 'Null is not allowed.', path, null); | ||
} // Run validations and produce a new value | ||
@@ -240,3 +270,3 @@ | ||
validators.forEach(test => { | ||
if (test.skipIfNull && value === null || test.skipIfOptional && !state.required && value === state.defaultValue) { | ||
if (test.skipIfNull && value === null || test.skipIfOptional && !state.required && value === state.defaultValue || state.never) { | ||
return; | ||
@@ -1069,3 +1099,3 @@ } | ||
if (process.env.NODE_ENV !== "production") { | ||
invariant(isObject(schemas) && Object.keys(schemas).length > 0 && Object.values(schemas).every(isSchema), 'A non-empty object of schemas are required for a shape.'); | ||
invariant(isObject(schemas) && Object.values(schemas).every(isSchema), 'An object of schemas are required for a blueprint.'); | ||
} | ||
@@ -1729,2 +1759,3 @@ | ||
let invalid; | ||
let prefix = ''; | ||
@@ -1740,2 +1771,3 @@ if (error instanceof OptimalError) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
@@ -1745,4 +1777,9 @@ | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
@@ -1749,0 +1786,0 @@ } |
{ | ||
"name": "optimal", | ||
"type": "module", | ||
"version": "5.0.0-alpha.3", | ||
"version": "5.0.0-alpha.4", | ||
"description": "A system for building and validating defined object structures with schemas.", | ||
@@ -57,3 +57,3 @@ "main": "./lib/node/index.js", | ||
}, | ||
"gitHead": "ff5624c9e6327990d91ad2d69797a6cebaeca67d" | ||
"gitHead": "d96228ccbc4dedda3f51667e9833563ed55ea26f" | ||
} |
@@ -42,3 +42,3 @@ import { invalid, tryAndCollect } from './helpers'; | ||
invalid(!state.required, 'Field is required and must be defined.', path); | ||
invalid(!state.required, 'Field is required and must be defined.', path, undefined); | ||
} else { | ||
@@ -55,3 +55,3 @@ if (__DEV__ && metadata.deprecatedMessage) { | ||
if (value === null) { | ||
invalid(state.nullable, 'Null is not allowed.', path); | ||
invalid(state.nullable, 'Null is not allowed.', path, null); | ||
} | ||
@@ -65,3 +65,4 @@ | ||
(test.skipIfNull && value === null) || | ||
(test.skipIfOptional && !state.required && value === state.defaultValue) | ||
(test.skipIfOptional && !state.required && value === state.defaultValue) || | ||
state.never | ||
) { | ||
@@ -68,0 +69,0 @@ return; |
@@ -19,4 +19,4 @@ import { invalid, invariant, isObject, isSchema, logUnknown, tryAndCollect } from '../helpers'; | ||
invariant( | ||
isObject(schemas) && Object.keys(schemas).length > 0 && Object.values(schemas).every(isSchema), | ||
'A non-empty object of schemas are required for a shape.', | ||
isObject(schemas) && Object.values(schemas).every(isSchema), | ||
'An object of schemas are required for a blueprint.', | ||
); | ||
@@ -23,0 +23,0 @@ |
@@ -137,2 +137,28 @@ import { Constructor, Schema, UnknownObject } from './types'; | ||
export function prettyValue(value: unknown): string | null { | ||
switch (typeof value) { | ||
case 'string': | ||
return `"${value}"`; | ||
case 'number': | ||
case 'function': | ||
return String(value); | ||
case 'object': { | ||
if (value === null) { | ||
return `\`null\``; | ||
} | ||
if (value.constructor !== Object) { | ||
return value.constructor.name === 'Array' ? null : `\`${value.constructor.name}\``; | ||
} | ||
return null; | ||
} | ||
default: | ||
return `\`${value}\``; | ||
} | ||
} | ||
export function tryAndCollect( | ||
@@ -139,0 +165,0 @@ validator: () => boolean | void, |
@@ -51,2 +51,3 @@ import { isObject } from './helpers'; | ||
let invalid: OptimalError; | ||
let prefix = ''; | ||
@@ -62,2 +63,3 @@ if (error instanceof OptimalError) { | ||
invalid.schema = options.name; | ||
prefix = options.name; | ||
} | ||
@@ -67,4 +69,9 @@ | ||
invalid.file = options.file; | ||
prefix = prefix ? `${prefix} (${options.file})` : options.file; | ||
} | ||
if (prefix) { | ||
invalid.message = `${prefix}: ${invalid.message}`; | ||
} | ||
throw invalid; | ||
@@ -71,0 +78,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { pathKey } from './helpers'; | ||
import { pathKey, prettyValue } from './helpers'; | ||
@@ -19,6 +19,9 @@ export class ValidationError extends Error { | ||
const key = pathKey(path); | ||
const valueLabel = prettyValue(value); | ||
const typeLabel = key.includes('[') ? 'member' : 'field'; | ||
const label = valueLabel | ||
? `Invalid ${typeLabel} "${key}" with value ${valueLabel}.` | ||
: `Invalid ${typeLabel} "${key}".`; | ||
this.message = `${ | ||
key.includes('[') ? `Invalid member "${key}".` : `Invalid field "${key}".` | ||
} ${this.message}`; | ||
this.message = `${label} ${this.message}`; | ||
} | ||
@@ -25,0 +28,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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
356309
6923