Comparing version 5.0.0 to 5.1.0
@@ -1,2 +0,2 @@ | ||
import { Criteria, CriteriaValidator, Options, Schema, SchemaState, ValueComparator } from '../types'; | ||
import { Criteria, CriteriaValidator, Options, Schema, SchemaState, Transformer, ValueComparator } from '../types'; | ||
/** | ||
@@ -51,2 +51,6 @@ * Map a list of field names that must be defined alongside this field when in a shape/object. | ||
/** | ||
* Transform a value before it's passed to the next criteria. | ||
*/ | ||
export declare function transform<T>(state: SchemaState<T>, transformer: Transformer<T>): Criteria<T>; | ||
/** | ||
* Validate with another schema when a condition is met. | ||
@@ -53,0 +57,0 @@ */ |
@@ -11,2 +11,3 @@ export declare type Primitive = bigint | boolean | number | string | symbol | null | undefined; | ||
export declare type WhenCondition<T> = T | ValueComparator<T>; | ||
export declare type Transformer<T> = (value: T) => T; | ||
export interface Options { | ||
@@ -48,2 +49,4 @@ /** Custom message when the field and criteria is invalid. */ | ||
required: (options?: Options) => S; | ||
/** Transform a value before it's passed to the next criteria. */ | ||
transform: (transformer: Transformer<InferSchemaType<S>>) => S; | ||
/** Validate with another schema when a condition is met. */ | ||
@@ -50,0 +53,0 @@ when: (condition: WhenCondition<InferSchemaType<S>>, pass: AnySchema, fail?: AnySchema) => S; |
201
esm/index.js
@@ -264,2 +264,4 @@ 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; } | ||
} else { | ||
var _metadata$neverMessag; | ||
if (process.env.NODE_ENV !== "production" && metadata.deprecatedMessage) { | ||
@@ -270,3 +272,3 @@ // eslint-disable-next-line no-console | ||
invalid(!state.never, metadata.neverMessage ?? 'Field should never be used.', path); | ||
invalid(!state.never, (_metadata$neverMessag = metadata.neverMessage) !== null && _metadata$neverMessag !== void 0 ? _metadata$neverMessag : 'Field should never be used.', path); | ||
} // Handle null | ||
@@ -276,3 +278,5 @@ | ||
if (value === null) { | ||
invalid(state.nullable, metadata.nullableMessage ?? 'Null is not allowed.', path, null); | ||
var _metadata$nullableMes; | ||
invalid(state.nullable, (_metadata$nullableMes = metadata.nullableMessage) !== null && _metadata$nullableMes !== void 0 ? _metadata$nullableMes : 'Null is not allowed.', path, null); | ||
} // Run validations and produce a new value | ||
@@ -382,3 +386,5 @@ | ||
validate(value, path) { | ||
invalid(value.length > 0, options.message ?? 'Array cannot be empty.', path, value); | ||
var _options$message; | ||
invalid(value.length > 0, (_options$message = options.message) !== null && _options$message !== void 0 ? _options$message : 'Array cannot be empty.', path, value); | ||
} | ||
@@ -427,3 +433,5 @@ | ||
validate(value, path) { | ||
invalid(value.length === size, options.message ?? `Array length must be ${size}, received ${value.length}.`, path, value); | ||
var _options$message2; | ||
invalid(value.length === size, (_options$message2 = options.message) !== null && _options$message2 !== void 0 ? _options$message2 : `Array length must be ${size}, received ${value.length}.`, path, value); | ||
} | ||
@@ -453,3 +461,5 @@ | ||
validate(value, path) { | ||
invalid(value === false, options.message ?? 'May only be `false`.', path, value); | ||
var _options$message3; | ||
invalid(value === false, (_options$message3 = options.message) !== null && _options$message3 !== void 0 ? _options$message3 : 'May only be `false`.', path, value); | ||
} | ||
@@ -471,3 +481,5 @@ | ||
validate(value, path) { | ||
invalid(value === true, options.message ?? 'May only be `true`.', path, value); | ||
var _options$message4; | ||
invalid(value === true, (_options$message4 = options.message) !== null && _options$message4 !== void 0 ? _options$message4 : 'May only be `true`.', path, value); | ||
} | ||
@@ -490,2 +502,4 @@ | ||
function of$4(state, ref, options = {}) { | ||
var _ref$name; | ||
if (process.env.NODE_ENV !== "production") { | ||
@@ -495,6 +509,8 @@ invariant(typeof ref === 'function', 'A class reference is required.'); | ||
state.type = ref.name ?? ref.constructor.name; | ||
state.type = (_ref$name = ref.name) !== null && _ref$name !== void 0 ? _ref$name : ref.constructor.name; | ||
return { | ||
validate(value, path) { | ||
invalid(typeof ref === 'function' && (value instanceof ref || !!options.loose && isObject(value) && instanceOf(value, ref)), options.message ?? `Must be an instance of \`${state.type}\`.`, path, value); | ||
var _options$message5; | ||
invalid(typeof ref === 'function' && (value instanceof ref || !!options.loose && isObject(value) && instanceOf(value, ref)), (_options$message5 = options.message) !== null && _options$message5 !== void 0 ? _options$message5 : `Must be an instance of \`${state.type}\`.`, path, value); | ||
} | ||
@@ -543,4 +559,6 @@ | ||
}) { | ||
var _options$message6; | ||
const andKeys = [...new Set([pathKey(path), ...keys])].sort(); | ||
const undefs = andKeys.filter(key => currentObject?.[key] == null); // Only error once when one of the struct is defined | ||
const undefs = andKeys.filter(key => (currentObject === null || currentObject === void 0 ? void 0 : currentObject[key]) == null); // Only error once when one of the struct is defined | ||
@@ -551,3 +569,3 @@ if (undefs.length === andKeys.length) { | ||
invalid(undefs.length === 0, options.message ?? `All of these fields must be defined: ${andKeys.join(', ')}`); | ||
invalid(undefs.length === 0, (_options$message6 = options.message) !== null && _options$message6 !== void 0 ? _options$message6 : `All of these fields must be defined: ${andKeys.join(', ')}`); | ||
} | ||
@@ -657,4 +675,6 @@ | ||
validate(value, path, validateOptions) { | ||
var _options$message7; | ||
const testValue = extractDefaultValue(defaultValue, path, validateOptions); | ||
invalid(value === testValue, options.message ?? `Value may only be "${testValue}".`, path, value); | ||
invalid(value === testValue, (_options$message7 = options.message) !== null && _options$message7 !== void 0 ? _options$message7 : `Value may only be "${testValue}".`, path, value); | ||
} | ||
@@ -681,5 +701,7 @@ | ||
}) { | ||
var _options$message8; | ||
const orKeys = [...new Set([pathKey(path), ...keys])].sort(); | ||
const defs = orKeys.filter(key => currentObject?.[key] !== undefined && currentObject?.[key] !== null); | ||
invalid(defs.length > 0, options.message ?? `At least one of these fields must be defined: ${orKeys.join(', ')}`); | ||
const defs = orKeys.filter(key => (currentObject === null || currentObject === void 0 ? void 0 : currentObject[key]) !== undefined && (currentObject === null || currentObject === void 0 ? void 0 : currentObject[key]) !== null); | ||
invalid(defs.length > 0, (_options$message8 = options.message) !== null && _options$message8 !== void 0 ? _options$message8 : `At least one of these fields must be defined: ${orKeys.join(', ')}`); | ||
} | ||
@@ -706,2 +728,23 @@ | ||
/** | ||
* Transform a value before it's passed to the next criteria. | ||
*/ | ||
function transform(state, transformer) { | ||
if (process.env.NODE_ENV !== "production") { | ||
invariant(typeof transformer === 'function', 'A function is required for transforming values.'); | ||
} | ||
return { | ||
validate(value, path) { | ||
const beforeType = typeOf(value); | ||
const nextValue = transformer(value); | ||
const afterType = typeOf(nextValue); | ||
invalid(afterType === beforeType, `Invalid transformed value, expected ${beforeType} but received ${afterType}.`, path, value); | ||
return nextValue; | ||
} | ||
}; | ||
} | ||
/** | ||
* Validate with another schema when a condition is met. | ||
@@ -759,5 +802,7 @@ */ | ||
}) { | ||
var _options$message9; | ||
const xorKeys = [...new Set([pathKey(path), ...keys])].sort(); | ||
const defs = xorKeys.filter(key => currentObject?.[key] !== undefined && currentObject?.[key] !== null); | ||
invalid(defs.length === 1, options.message ?? `Only one of these fields may be defined: ${xorKeys.join(', ')}`); | ||
const defs = xorKeys.filter(key => (currentObject === null || currentObject === void 0 ? void 0 : currentObject[key]) !== undefined && (currentObject === null || currentObject === void 0 ? void 0 : currentObject[key]) !== null); | ||
invalid(defs.length === 1, (_options$message9 = options.message) !== null && _options$message9 !== void 0 ? _options$message9 : `Only one of these fields may be defined: ${xorKeys.join(', ')}`); | ||
} | ||
@@ -782,2 +827,3 @@ | ||
notUndefinable: notUndefinable, | ||
transform: transform, | ||
when: when, | ||
@@ -799,3 +845,5 @@ xor: xor | ||
validate(value, path) { | ||
invalid(isValidDate(value) && value > afterDate, options.message ?? `Date must come after ${prettyValue(afterDate)}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message10; | ||
invalid(isValidDate(value) && value > afterDate, (_options$message10 = options.message) !== null && _options$message10 !== void 0 ? _options$message10 : `Date must come after ${prettyValue(afterDate)}, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -819,3 +867,5 @@ | ||
validate(value, path) { | ||
invalid(isValidDate(value) && value < beforeDate, options.message ?? `Date must come before ${prettyValue(beforeDate)}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message11; | ||
invalid(isValidDate(value) && value < beforeDate, (_options$message11 = options.message) !== null && _options$message11 !== void 0 ? _options$message11 : `Date must come before ${prettyValue(beforeDate)}, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -844,3 +894,5 @@ | ||
validate(value, path) { | ||
invalid(isValidDate(value) && (options.inclusive ? value >= startDate && value <= endDate : value > startDate && value < endDate), options.message ?? `Date must be between ${prettyValue(startDate)} and ${prettyValue(endDate)}${options.inclusive ? ' inclusive' : ''}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message12; | ||
invalid(isValidDate(value) && (options.inclusive ? value >= startDate && value <= endDate : value > startDate && value < endDate), (_options$message12 = options.message) !== null && _options$message12 !== void 0 ? _options$message12 : `Date must be between ${prettyValue(startDate)} and ${prettyValue(endDate)}${options.inclusive ? ' inclusive' : ''}, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -868,3 +920,5 @@ | ||
validate(value, path) { | ||
invalid(isValidNumber(value) && (options.inclusive ? value >= min && value <= max : value > min && value < max), options.message ?? `Number must be between ${min} and ${max}${options.inclusive ? ' inclusive' : ''}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message13; | ||
invalid(isValidNumber(value) && (options.inclusive ? value >= min && value <= max : value > min && value < max), (_options$message13 = options.message) !== null && _options$message13 !== void 0 ? _options$message13 : `Number must be between ${min} and ${max}${options.inclusive ? ' inclusive' : ''}, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -882,3 +936,5 @@ | ||
validate(value, path) { | ||
invalid(isValidNumber(value) && value % 1 !== 0, options.message ?? `Number must be a float, received ${prettyValue(value)}.`, path, value); | ||
var _options$message14; | ||
invalid(isValidNumber(value) && value % 1 !== 0, (_options$message14 = options.message) !== null && _options$message14 !== void 0 ? _options$message14 : `Number must be a float, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -901,5 +957,9 @@ | ||
if (options.inclusive) { | ||
invalid(isValidNumber(value) && value >= min, options.message ?? `Number must be greater than or equal to ${min}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message15; | ||
invalid(isValidNumber(value) && value >= min, (_options$message15 = options.message) !== null && _options$message15 !== void 0 ? _options$message15 : `Number must be greater than or equal to ${min}, received ${prettyValue(value)}.`, path, value); | ||
} else { | ||
invalid(isValidNumber(value) && value > min, options.message ?? `Number must be greater than ${min}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message16; | ||
invalid(isValidNumber(value) && value > min, (_options$message16 = options.message) !== null && _options$message16 !== void 0 ? _options$message16 : `Number must be greater than ${min}, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -928,3 +988,5 @@ } | ||
validate(value, path) { | ||
invalid(Number.isSafeInteger(value), options.message ?? `Number must be an integer, received ${prettyValue(value)}.`, path, value); | ||
var _options$message17; | ||
invalid(Number.isSafeInteger(value), (_options$message17 = options.message) !== null && _options$message17 !== void 0 ? _options$message17 : `Number must be an integer, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -947,5 +1009,9 @@ | ||
if (options.inclusive) { | ||
invalid(isValidNumber(value) && value <= max, options.message ?? `Number must be less than or equal to ${max}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message18; | ||
invalid(isValidNumber(value) && value <= max, (_options$message18 = options.message) !== null && _options$message18 !== void 0 ? _options$message18 : `Number must be less than or equal to ${max}, received ${prettyValue(value)}.`, path, value); | ||
} else { | ||
invalid(isValidNumber(value) && value < max, options.message ?? `Number must be less than ${max}, received ${prettyValue(value)}.`, path, value); | ||
var _options$message19; | ||
invalid(isValidNumber(value) && value < max, (_options$message19 = options.message) !== null && _options$message19 !== void 0 ? _options$message19 : `Number must be less than ${max}, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -974,3 +1040,5 @@ } | ||
validate(value, path) { | ||
invalid(isValidNumber(value) && value < 0, options.message ?? `Number must be negative, received ${prettyValue(value)}.`, path, value); | ||
var _options$message20; | ||
invalid(isValidNumber(value) && value < 0, (_options$message20 = options.message) !== null && _options$message20 !== void 0 ? _options$message20 : `Number must be negative, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -992,3 +1060,5 @@ | ||
validate(value, path) { | ||
invalid(list.includes(value), options.message ?? `Number must be one of: ${list.join(', ')}. Received ${prettyValue(value)}.`, path, value); | ||
var _options$message21; | ||
invalid(list.includes(value), (_options$message21 = options.message) !== null && _options$message21 !== void 0 ? _options$message21 : `Number must be one of: ${list.join(', ')}. Received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -1006,3 +1076,5 @@ | ||
validate(value, path) { | ||
invalid(isValidNumber(value) && value > 0, options.message ?? `Number must be positive, received ${prettyValue(value)}.`, path, value); | ||
var _options$message22; | ||
invalid(isValidNumber(value) && value > 0, (_options$message22 = options.message) !== null && _options$message22 !== void 0 ? _options$message22 : `Number must be positive, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -1044,3 +1116,5 @@ | ||
if (error instanceof Error) { | ||
invalid(false, `Invalid key "${key}". ${options.message ?? error.message}`, path, value); | ||
var _options$message23; | ||
invalid(false, `Invalid key "${key}". ${(_options$message23 = options.message) !== null && _options$message23 !== void 0 ? _options$message23 : error.message}`, path, value); | ||
} | ||
@@ -1062,3 +1136,5 @@ } | ||
validate(value, path) { | ||
invalid(Object.keys(value).length > 0, options.message ?? 'Object cannot be empty.', path, value); | ||
var _options$message24; | ||
invalid(Object.keys(value).length > 0, (_options$message24 = options.message) !== null && _options$message24 !== void 0 ? _options$message24 : 'Object cannot be empty.', path, value); | ||
} | ||
@@ -1109,6 +1185,8 @@ | ||
validate(value, path) { | ||
var _options$message25; | ||
const { | ||
length | ||
} = Object.keys(value); | ||
invalid(length === size, options.message ?? (size === 1 ? `Object must have ${size} property, received ${length}.` : `Object must have ${size} properties, received ${length}.`), path, value); | ||
invalid(length === size, (_options$message25 = options.message) !== null && _options$message25 !== void 0 ? _options$message25 : size === 1 ? `Object must have ${size} property, received ${length}.` : `Object must have ${size} properties, received ${length}.`, path, value); | ||
} | ||
@@ -1169,3 +1247,5 @@ | ||
if (schemaState.required) { | ||
invalid(key in value && value[key] !== undefined, schemaState.metadata.requiredMessage ?? 'Field is required and must be defined.', currentPath, undefined); | ||
var _schemaState$metadata; | ||
invalid(key in value && value[key] !== undefined, (_schemaState$metadata = schemaState.metadata.requiredMessage) !== null && _schemaState$metadata !== void 0 ? _schemaState$metadata : 'Field is required and must be defined.', currentPath, undefined); | ||
} | ||
@@ -1220,3 +1300,5 @@ | ||
validate(value, path) { | ||
invalid(value.includes(token, options.index ?? 0), options.message ?? `String does not include "${token}".`, path, value); | ||
var _options$index, _options$message26; | ||
invalid(value.includes(token, (_options$index = options.index) !== null && _options$index !== void 0 ? _options$index : 0), (_options$message26 = options.message) !== null && _options$message26 !== void 0 ? _options$message26 : `String does not include "${token}".`, path, value); | ||
} | ||
@@ -1238,3 +1320,5 @@ | ||
validate(value, path) { | ||
invalid(!!value.match(pattern), `${options.message ?? 'String does not match.'} (pattern "${pattern.source}")`, path, value); | ||
var _options$message27; | ||
invalid(!!value.match(pattern), `${(_options$message27 = options.message) !== null && _options$message27 !== void 0 ? _options$message27 : 'String does not match.'} (pattern "${pattern.source}")`, path, value); | ||
} | ||
@@ -1296,3 +1380,5 @@ | ||
validate(value, path) { | ||
invalid(isValidString(value), options.message ?? 'String cannot be empty.', path, value); | ||
var _options$message28; | ||
invalid(isValidString(value), (_options$message28 = options.message) !== null && _options$message28 !== void 0 ? _options$message28 : 'String cannot be empty.', path, value); | ||
} | ||
@@ -1314,3 +1400,5 @@ | ||
validate(value, path) { | ||
invalid(list.includes(value), options.message ?? `String must be one of: ${list.join(', ')}. Received ${prettyValue(value)}.`, path, value); | ||
var _options$message29; | ||
invalid(list.includes(value), (_options$message29 = options.message) !== null && _options$message29 !== void 0 ? _options$message29 : `String must be one of: ${list.join(', ')}. Received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -1328,3 +1416,5 @@ | ||
validate(value, path) { | ||
invalid(value === value.toLocaleLowerCase(), options.message ?? `String must be lower cased, received ${prettyValue(value)}.`, path, value); | ||
var _options$message30; | ||
invalid(value === value.toLocaleLowerCase(), (_options$message30 = options.message) !== null && _options$message30 !== void 0 ? _options$message30 : `String must be lower cased, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -1342,3 +1432,5 @@ | ||
validate(value, path) { | ||
invalid(value === value.toLocaleUpperCase(), options.message ?? `String must be upper cased, received ${prettyValue(value)}.`, path, value); | ||
var _options$message31; | ||
invalid(value === value.toLocaleUpperCase(), (_options$message31 = options.message) !== null && _options$message31 !== void 0 ? _options$message31 : `String must be upper cased, received ${prettyValue(value)}.`, path, value); | ||
} | ||
@@ -1360,3 +1452,5 @@ | ||
validate(value, path) { | ||
invalid(value.length === size, options.message ?? `String length must be ${size}, received ${value.length}.`, path, value); | ||
var _options$message32; | ||
invalid(value.length === size, (_options$message32 = options.message) !== null && _options$message32 !== void 0 ? _options$message32 : `String length must be ${size}, received ${value.length}.`, path, value); | ||
} | ||
@@ -1466,3 +1560,5 @@ | ||
} else { | ||
invalid(false, options.message ?? `Received ${valueType} but value must be one of: ${allowedValues}.`, path, value); | ||
var _options$message33; | ||
invalid(false, (_options$message33 = options.message) !== null && _options$message33 !== void 0 ? _options$message33 : `Received ${valueType} but value must be one of: ${allowedValues}.`, path, value); | ||
} | ||
@@ -1511,3 +1607,3 @@ } | ||
cast: createObject, | ||
defaultValue: defaultValue ?? {}, | ||
defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : {}, | ||
type: 'object' | ||
@@ -1546,3 +1642,8 @@ }, [{ | ||
validate(value, path) { | ||
invalid(typeof value === 'function', `Must be a function, received ${typeOf(value)}.`, path, value); | ||
// Special case, even when undefinable. See note above! | ||
if (value !== undefined) { | ||
invalid(typeof value === 'function', `Must be a function, received ${typeOf(value)}.`, path, value); | ||
} | ||
return value; | ||
} | ||
@@ -1573,6 +1674,6 @@ | ||
return shape.of({ | ||
schema: func().notNullable().notUndefinable(), | ||
state: func().notNullable().notUndefinable(), | ||
type: func().notNullable().notUndefinable(), | ||
validate: func().notNullable().notUndefinable() | ||
schema: func().notNullable().notUndefinable().required(), | ||
state: func().notNullable().notUndefinable().required(), | ||
type: func().notNullable().notUndefinable().required(), | ||
validate: func().notNullable().notUndefinable().required() | ||
}); | ||
@@ -1635,3 +1736,3 @@ } | ||
cast: createDate, | ||
defaultValue: defaultValue ?? new Date(), | ||
defaultValue: defaultValue !== null && defaultValue !== void 0 ? defaultValue : new Date(), | ||
type: 'date' | ||
@@ -1799,3 +1900,3 @@ }, [{ | ||
function uuid(version) { | ||
return string().notEmpty().match(new RegExp(`^([0-9a-f]{8}-[0-9a-f]{4}-[${version ?? '1-5'}][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})|([0]{8}-[0]{4}-[0]{4}-[0]{4}-[0]{12})$`, 'i')).notNullable().notUndefinable(); | ||
return string().notEmpty().match(new RegExp(`^([0-9a-f]{8}-[0-9a-f]{4}-[${version !== null && version !== void 0 ? version : '1-5'}][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12})|([0]{8}-[0]{4}-[0]{4}-[0]{4}-[0]{12})$`, 'i')).notNullable().notUndefinable(); | ||
} | ||
@@ -1909,3 +2010,5 @@ /** | ||
try { | ||
return schema.validate(struct, options.prefix ?? '', { ...validateOptions, | ||
var _options$prefix; | ||
return schema.validate(struct, (_options$prefix = options.prefix) !== null && _options$prefix !== void 0 ? _options$prefix : '', { ...validateOptions, | ||
currentObject: object, | ||
@@ -1912,0 +2015,0 @@ rootObject: object |
@@ -720,2 +720,23 @@ // Bundled with Packemon: https://packemon.dev | ||
/** | ||
* Transform a value before it's passed to the next criteria. | ||
*/ | ||
function transform(state, transformer) { | ||
if (process.env.NODE_ENV !== "production") { | ||
invariant(typeof transformer === 'function', 'A function is required for transforming values.'); | ||
} | ||
return { | ||
validate(value, path) { | ||
const beforeType = typeOf(value); | ||
const nextValue = transformer(value); | ||
const afterType = typeOf(nextValue); | ||
invalid(afterType === beforeType, `Invalid transformed value, expected ${beforeType} but received ${afterType}.`, path, value); | ||
return nextValue; | ||
} | ||
}; | ||
} | ||
/** | ||
* Validate with another schema when a condition is met. | ||
@@ -797,2 +818,3 @@ */ | ||
notUndefinable: notUndefinable, | ||
transform: transform, | ||
when: when, | ||
@@ -1587,3 +1609,8 @@ xor: xor | ||
validate(value, path) { | ||
invalid(typeof value === 'function', `Must be a function, received ${typeOf(value)}.`, path, value); | ||
// Special case, even when undefinable. See note above! | ||
if (value !== undefined) { | ||
invalid(typeof value === 'function', `Must be a function, received ${typeOf(value)}.`, path, value); | ||
} | ||
return value; | ||
} | ||
@@ -1614,6 +1641,6 @@ | ||
return shape.of({ | ||
schema: func().notNullable().notUndefinable(), | ||
state: func().notNullable().notUndefinable(), | ||
type: func().notNullable().notUndefinable(), | ||
validate: func().notNullable().notUndefinable() | ||
schema: func().notNullable().notUndefinable().required(), | ||
state: func().notNullable().notUndefinable().required(), | ||
type: func().notNullable().notUndefinable().required(), | ||
validate: func().notNullable().notUndefinable().required() | ||
}); | ||
@@ -1620,0 +1647,0 @@ } |
@@ -719,2 +719,23 @@ // Bundled with Packemon: https://packemon.dev | ||
/** | ||
* Transform a value before it's passed to the next criteria. | ||
*/ | ||
function transform(state, transformer) { | ||
if (process.env.NODE_ENV !== "production") { | ||
invariant(typeof transformer === 'function', 'A function is required for transforming values.'); | ||
} | ||
return { | ||
validate(value, path) { | ||
const beforeType = typeOf(value); | ||
const nextValue = transformer(value); | ||
const afterType = typeOf(nextValue); | ||
invalid(afterType === beforeType, `Invalid transformed value, expected ${beforeType} but received ${afterType}.`, path, value); | ||
return nextValue; | ||
} | ||
}; | ||
} | ||
/** | ||
* Validate with another schema when a condition is met. | ||
@@ -796,2 +817,3 @@ */ | ||
notUndefinable: notUndefinable, | ||
transform: transform, | ||
when: when, | ||
@@ -1578,3 +1600,8 @@ xor: xor | ||
validate(value, path) { | ||
invalid(typeof value === 'function', `Must be a function, received ${typeOf(value)}.`, path, value); | ||
// Special case, even when undefinable. See note above! | ||
if (value !== undefined) { | ||
invalid(typeof value === 'function', `Must be a function, received ${typeOf(value)}.`, path, value); | ||
} | ||
return value; | ||
} | ||
@@ -1603,6 +1630,6 @@ | ||
return shape.of({ | ||
schema: func().notNullable().notUndefinable(), | ||
state: func().notNullable().notUndefinable(), | ||
type: func().notNullable().notUndefinable(), | ||
validate: func().notNullable().notUndefinable() | ||
schema: func().notNullable().notUndefinable().required(), | ||
state: func().notNullable().notUndefinable().required(), | ||
type: func().notNullable().notUndefinable().required(), | ||
validate: func().notNullable().notUndefinable().required() | ||
}); | ||
@@ -1609,0 +1636,0 @@ } |
{ | ||
"name": "optimal", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Build, validate, and transform values with immutable typed schemas.", | ||
@@ -56,3 +56,3 @@ "main": "./lib/node/index.js", | ||
}, | ||
"gitHead": "c86bfc388a9aab6367e8bf00ed0d6679b89cf26d" | ||
"gitHead": "f25b4ebe01b43495907a30ef62add64fc5724d1a" | ||
} |
@@ -8,2 +8,3 @@ import { | ||
pathKey, | ||
typeOf, | ||
} from '../helpers'; | ||
@@ -17,2 +18,3 @@ import { OptimalError } from '../OptimalError'; | ||
SchemaState, | ||
Transformer, | ||
ValueComparator, | ||
@@ -182,2 +184,26 @@ } from '../types'; | ||
/** | ||
* Transform a value before it's passed to the next criteria. | ||
*/ | ||
export function transform<T>(state: SchemaState<T>, transformer: Transformer<T>): Criteria<T> { | ||
invariant(typeof transformer === 'function', 'A function is required for transforming values.'); | ||
return { | ||
validate(value, path) { | ||
const beforeType = typeOf(value); | ||
const nextValue = transformer(value); | ||
const afterType = typeOf(nextValue); | ||
invalid( | ||
afterType === beforeType, | ||
`Invalid transformed value, expected ${beforeType} but received ${afterType}.`, | ||
path, | ||
value, | ||
); | ||
return nextValue; | ||
}, | ||
}; | ||
} | ||
/** | ||
* Validate with another schema when a condition is met. | ||
@@ -184,0 +210,0 @@ */ |
@@ -52,8 +52,13 @@ import { createSchema } from '../createSchema'; | ||
validate(value, path) { | ||
invalid( | ||
typeof value === 'function', | ||
`Must be a function, received ${typeOf(value)}.`, | ||
path, | ||
value, | ||
); | ||
// Special case, even when undefinable. See note above! | ||
if (value !== undefined) { | ||
invalid( | ||
typeof value === 'function', | ||
`Must be a function, received ${typeOf(value)}.`, | ||
path, | ||
value, | ||
); | ||
} | ||
return value; | ||
}, | ||
@@ -60,0 +65,0 @@ }, |
@@ -30,7 +30,7 @@ import { createSchema } from '../createSchema'; | ||
return shape.of({ | ||
schema: func<T['schema']>().notNullable().notUndefinable(), | ||
state: func<T['state']>().notNullable().notUndefinable(), | ||
type: func<T['type']>().notNullable().notUndefinable(), | ||
validate: func<T['validate']>().notNullable().notUndefinable(), | ||
schema: func<T['schema']>().notNullable().notUndefinable().required(), | ||
state: func<T['state']>().notNullable().notUndefinable().required(), | ||
type: func<T['type']>().notNullable().notUndefinable().required(), | ||
validate: func<T['validate']>().notNullable().notUndefinable().required(), | ||
}) as unknown as ShapeSchema<T>; | ||
} |
@@ -29,2 +29,4 @@ export type Primitive = bigint | boolean | number | string | symbol | null | undefined; | ||
export type Transformer<T> = (value: T) => T; | ||
// CRITERIA OPTIONS | ||
@@ -84,2 +86,4 @@ | ||
required: (options?: Options) => S; | ||
/** Transform a value before it's passed to the next criteria. */ | ||
transform: (transformer: Transformer<InferSchemaType<S>>) => S; | ||
/** Validate with another schema when a condition is met. */ | ||
@@ -86,0 +90,0 @@ when: (condition: WhenCondition<InferSchemaType<S>>, pass: AnySchema, fail?: AnySchema) => S; |
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
431159
8214