superstruct
Advanced tools
Comparing version 0.13.3 to 0.14.0
@@ -5,2 +5,10 @@ # Changelog | ||
### `0.14.0` — January 26, 2021 | ||
###### BREAKING | ||
**The `mask` helper now works for nested objects.** Previously it would only mask the properties at the top-level of a struct, however now it acts deeply. You can use it to define object structs once, but use them either strictly or loosely. | ||
**The `masked` coercion has been removed.** This previously allowed you to mix in masking to a specific struct, but the `mask` helper is a more robust way to do this, and it doesn't force you to maintain two separate structs. | ||
### `0.13.0` — December 11, 2020 | ||
@@ -7,0 +15,0 @@ |
@@ -214,2 +214,3 @@ /** | ||
coerce?: boolean; | ||
mask?: boolean; | ||
}): [ | ||
@@ -276,9 +277,2 @@ StructError, | ||
/** | ||
* Augment a struct to mask its input to only properties defined in the struct. | ||
* | ||
* Note: You must use `create(value, Struct)` on the value to have the coercion | ||
* take effect! Using simply `assert()` or `is()` will not use coercion. | ||
*/ | ||
declare function masked<T, S>(struct: Struct<T, S>): Struct<T, S>; | ||
/** | ||
* Augment a struct to trim string inputs. | ||
@@ -1243,2 +1237,2 @@ * | ||
declare function struct<T>(name: string, validator: Validator): Struct<T, null>; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, masked, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; |
@@ -214,2 +214,3 @@ /** | ||
coerce?: boolean; | ||
mask?: boolean; | ||
}): [ | ||
@@ -276,9 +277,2 @@ StructError, | ||
/** | ||
* Augment a struct to mask its input to only properties defined in the struct. | ||
* | ||
* Note: You must use `create(value, Struct)` on the value to have the coercion | ||
* take effect! Using simply `assert()` or `is()` will not use coercion. | ||
*/ | ||
declare function masked<T, S>(struct: Struct<T, S>): Struct<T, S>; | ||
/** | ||
* Augment a struct to trim string inputs. | ||
@@ -1243,2 +1237,2 @@ * | ||
declare function struct<T>(name: string, validator: Validator): Struct<T, null>; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, masked, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; |
@@ -145,3 +145,4 @@ /** | ||
branch = [value], | ||
coerce = false | ||
coerce = false, | ||
mask = false | ||
} = options; | ||
@@ -155,2 +156,10 @@ const ctx = { | ||
value = struct.coercer(value, ctx); | ||
if (mask && struct.type !== 'type' && isObject(struct.schema) && isObject(value) && !Array.isArray(value)) { | ||
for (const key in value) { | ||
if (struct.schema[key] === undefined) { | ||
delete value[key]; | ||
} | ||
} | ||
} | ||
} | ||
@@ -169,3 +178,4 @@ | ||
branch: k === undefined ? branch : [...branch, v], | ||
coerce | ||
coerce, | ||
mask | ||
}); | ||
@@ -205,2 +215,163 @@ | ||
/** | ||
* `Struct` objects encapsulate the validation logic for a specific type of | ||
* values. Once constructed, you use the `assert`, `is` or `validate` helpers to | ||
* validate unknown input data against the struct. | ||
*/ | ||
class Struct { | ||
constructor(props) { | ||
const { | ||
type, | ||
schema, | ||
validator, | ||
refiner, | ||
coercer = value => value, | ||
entries = function* () {} | ||
} = props; | ||
this.type = type; | ||
this.schema = schema; | ||
this.entries = entries; | ||
this.coercer = coercer; | ||
if (validator) { | ||
this.validator = (value, context) => { | ||
const result = validator(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.validator = () => []; | ||
} | ||
if (refiner) { | ||
this.refiner = (value, context) => { | ||
const result = refiner(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.refiner = () => []; | ||
} | ||
} | ||
/** | ||
* Assert that a value passes the struct's validation, throwing if it doesn't. | ||
*/ | ||
assert(value) { | ||
return assert(value, this); | ||
} | ||
/** | ||
* Create a value with the struct's coercion logic, then validate it. | ||
*/ | ||
create(value) { | ||
return create(value, this); | ||
} | ||
/** | ||
* Check if a value passes the struct's validation. | ||
*/ | ||
is(value) { | ||
return is(value, this); | ||
} | ||
/** | ||
* Mask a value, coercing and validating it, but returning only the subset of | ||
* properties defined by the struct's schema. | ||
*/ | ||
mask(value) { | ||
return mask(value, this); | ||
} | ||
/** | ||
* Validate a value with the struct's validation logic, returning a tuple | ||
* representing the result. | ||
* | ||
* You may optionally pass `true` for the `withCoercion` argument to coerce | ||
* the value before attempting to validate it. If you do, the result will | ||
* contain the coerced result when successful. | ||
*/ | ||
validate(value, options = {}) { | ||
return validate(value, this, options); | ||
} | ||
} | ||
/** | ||
* Assert that a value passes a struct, throwing if it doesn't. | ||
*/ | ||
function assert(value, struct) { | ||
const result = validate(value, struct); | ||
if (result[0]) { | ||
throw result[0]; | ||
} | ||
} | ||
/** | ||
* Create a value with the coercion logic of struct and validate it. | ||
*/ | ||
function create(value, struct) { | ||
const result = validate(value, struct, { | ||
coerce: true | ||
}); | ||
if (result[0]) { | ||
throw result[0]; | ||
} else { | ||
return result[1]; | ||
} | ||
} | ||
/** | ||
* Mask a value, returning only the subset of properties defined by a struct. | ||
*/ | ||
function mask(value, struct) { | ||
const result = validate(value, struct, { | ||
coerce: true, | ||
mask: true | ||
}); | ||
if (result[0]) { | ||
throw result[0]; | ||
} else { | ||
return result[1]; | ||
} | ||
} | ||
/** | ||
* Check if a value passes a struct. | ||
*/ | ||
function is(value, struct) { | ||
const result = validate(value, struct); | ||
return !result[0]; | ||
} | ||
/** | ||
* Validate a value against a struct, returning an error if invalid, or the | ||
* value (with potential coercion) if valid. | ||
*/ | ||
function validate(value, struct, options = {}) { | ||
const tuples = run(value, struct, options); | ||
const tuple = shiftIterator(tuples); | ||
if (tuple[0]) { | ||
const error = new StructError(tuple[0], function* () { | ||
for (const t of tuples) { | ||
if (t[0]) { | ||
yield t[0]; | ||
} | ||
} | ||
}); | ||
return [error, undefined]; | ||
} else { | ||
const v = tuple[1]; | ||
return [undefined, v]; | ||
} | ||
} | ||
function assign(...Structs) { | ||
@@ -790,26 +961,2 @@ const schemas = Structs.map(s => s.schema); | ||
/** | ||
* Augment a struct to mask its input to only properties defined in the struct. | ||
* | ||
* Note: You must use `create(value, Struct)` on the value to have the coercion | ||
* take effect! Using simply `assert()` or `is()` will not use coercion. | ||
*/ | ||
function masked(struct) { | ||
return coerce(struct, unknown(), x => { | ||
if (typeof struct.schema !== 'object' || struct.schema == null || typeof x !== 'object' || x == null) { | ||
return x; | ||
} else { | ||
const ret = {}; | ||
for (const key in struct.schema) { | ||
if (key in x) { | ||
ret[key] = x[key]; | ||
} | ||
} | ||
return ret; | ||
} | ||
}); | ||
} | ||
/** | ||
* Augment a struct to trim string inputs. | ||
@@ -826,156 +973,2 @@ * | ||
/** | ||
* `Struct` objects encapsulate the validation logic for a specific type of | ||
* values. Once constructed, you use the `assert`, `is` or `validate` helpers to | ||
* validate unknown input data against the struct. | ||
*/ | ||
class Struct { | ||
constructor(props) { | ||
const { | ||
type, | ||
schema, | ||
validator, | ||
refiner, | ||
coercer = value => value, | ||
entries = function* () {} | ||
} = props; | ||
this.type = type; | ||
this.schema = schema; | ||
this.entries = entries; | ||
this.coercer = coercer; | ||
if (validator) { | ||
this.validator = (value, context) => { | ||
const result = validator(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.validator = () => []; | ||
} | ||
if (refiner) { | ||
this.refiner = (value, context) => { | ||
const result = refiner(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.refiner = () => []; | ||
} | ||
} | ||
/** | ||
* Assert that a value passes the struct's validation, throwing if it doesn't. | ||
*/ | ||
assert(value) { | ||
return assert(value, this); | ||
} | ||
/** | ||
* Create a value with the struct's coercion logic, then validate it. | ||
*/ | ||
create(value) { | ||
return create(value, this); | ||
} | ||
/** | ||
* Check if a value passes the struct's validation. | ||
*/ | ||
is(value) { | ||
return is(value, this); | ||
} | ||
/** | ||
* Mask a value, coercing and validating it, but returning only the subset of | ||
* properties defined by the struct's schema. | ||
*/ | ||
mask(value) { | ||
return mask(value, this); | ||
} | ||
/** | ||
* Validate a value with the struct's validation logic, returning a tuple | ||
* representing the result. | ||
* | ||
* You may optionally pass `true` for the `withCoercion` argument to coerce | ||
* the value before attempting to validate it. If you do, the result will | ||
* contain the coerced result when successful. | ||
*/ | ||
validate(value, options = {}) { | ||
return validate(value, this, options); | ||
} | ||
} | ||
/** | ||
* Assert that a value passes a struct, throwing if it doesn't. | ||
*/ | ||
function assert(value, struct) { | ||
const result = validate(value, struct); | ||
if (result[0]) { | ||
throw result[0]; | ||
} | ||
} | ||
/** | ||
* Create a value with the coercion logic of struct and validate it. | ||
*/ | ||
function create(value, struct) { | ||
const result = validate(value, struct, { | ||
coerce: true | ||
}); | ||
if (result[0]) { | ||
throw result[0]; | ||
} else { | ||
return result[1]; | ||
} | ||
} | ||
/** | ||
* Mask a value, returning only the subset of properties defined by a struct. | ||
*/ | ||
function mask(value, struct) { | ||
const M = masked(struct); | ||
const ret = create(value, M); | ||
return ret; | ||
} | ||
/** | ||
* Check if a value passes a struct. | ||
*/ | ||
function is(value, struct) { | ||
const result = validate(value, struct); | ||
return !result[0]; | ||
} | ||
/** | ||
* Validate a value against a struct, returning an error if invalid, or the | ||
* value (with potential coercion) if valid. | ||
*/ | ||
function validate(value, struct, options = {}) { | ||
const tuples = run(value, struct, options); | ||
const tuple = shiftIterator(tuples); | ||
if (tuple[0]) { | ||
const error = new StructError(tuple[0], function* () { | ||
for (const t of tuples) { | ||
if (t[0]) { | ||
yield t[0]; | ||
} | ||
} | ||
}); | ||
return [error, undefined]; | ||
} else { | ||
const v = tuple[1]; | ||
return [undefined, v]; | ||
} | ||
} | ||
/** | ||
* Ensure that a string, array, map, or set is empty. | ||
@@ -1082,3 +1075,3 @@ */ | ||
export { Struct, StructError, any, array, assert, assign, boolean, coerce, create, date, defaulted, define, dynamic, empty, enums, func, instance, integer, intersection, is, lazy, literal, map, mask, masked, max, min, never, nullable, number, object, omit, optional, partial, pattern, pick, record, refine, regexp, set, size, string, struct, trimmed, tuple, type, union, unknown, validate }; | ||
export { Struct, StructError, any, array, assert, assign, boolean, coerce, create, date, defaulted, define, dynamic, empty, enums, func, instance, integer, intersection, is, lazy, literal, map, mask, max, min, never, nullable, number, object, omit, optional, partial, pattern, pick, record, refine, regexp, set, size, string, struct, trimmed, tuple, type, union, unknown, validate }; | ||
//# sourceMappingURL=index.es.js.map |
@@ -75,2 +75,3 @@ import { StructSchema } from './utils'; | ||
coerce?: boolean; | ||
mask?: boolean; | ||
}): [StructError, undefined] | [undefined, T]; | ||
@@ -77,0 +78,0 @@ /** |
@@ -23,9 +23,2 @@ import { Struct, Coercer } from '../struct'; | ||
/** | ||
* Augment a struct to mask its input to only properties defined in the struct. | ||
* | ||
* Note: You must use `create(value, Struct)` on the value to have the coercion | ||
* take effect! Using simply `assert()` or `is()` will not use coercion. | ||
*/ | ||
export declare function masked<T, S>(struct: Struct<T, S>): Struct<T, S>; | ||
/** | ||
* Augment a struct to trim string inputs. | ||
@@ -32,0 +25,0 @@ * |
@@ -38,2 +38,3 @@ import { Struct, Infer, Result, Context, Describe } from './struct'; | ||
coerce?: boolean; | ||
mask?: boolean; | ||
}): IterableIterator<[Failure, undefined] | [undefined, T]>; | ||
@@ -40,0 +41,0 @@ /** |
@@ -5,3 +5,3 @@ { | ||
"description": "A simple and composable way to validate data in JavaScript (and TypeScript).", | ||
"version": "0.13.3", | ||
"version": "0.14.0", | ||
"license": "MIT", | ||
@@ -36,3 +36,3 @@ "repository": "git://github.com/ianstormtaylor/superstruct.git", | ||
"eslint": "^7.14.0", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-config-prettier": "^7.2.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
@@ -39,0 +39,0 @@ "eslint-plugin-prettier": "^3.1.4", |
@@ -214,2 +214,3 @@ /** | ||
coerce?: boolean; | ||
mask?: boolean; | ||
}): [ | ||
@@ -276,9 +277,2 @@ StructError, | ||
/** | ||
* Augment a struct to mask its input to only properties defined in the struct. | ||
* | ||
* Note: You must use `create(value, Struct)` on the value to have the coercion | ||
* take effect! Using simply `assert()` or `is()` will not use coercion. | ||
*/ | ||
declare function masked<T, S>(struct: Struct<T, S>): Struct<T, S>; | ||
/** | ||
* Augment a struct to trim string inputs. | ||
@@ -1243,2 +1237,2 @@ * | ||
declare function struct<T>(name: string, validator: Validator): Struct<T, null>; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, masked, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; |
@@ -151,3 +151,4 @@ (function (global, factory) { | ||
branch = [value], | ||
coerce = false | ||
coerce = false, | ||
mask = false | ||
} = options; | ||
@@ -161,2 +162,10 @@ const ctx = { | ||
value = struct.coercer(value, ctx); | ||
if (mask && struct.type !== 'type' && isObject(struct.schema) && isObject(value) && !Array.isArray(value)) { | ||
for (const key in value) { | ||
if (struct.schema[key] === undefined) { | ||
delete value[key]; | ||
} | ||
} | ||
} | ||
} | ||
@@ -175,3 +184,4 @@ | ||
branch: k === undefined ? branch : [...branch, v], | ||
coerce | ||
coerce, | ||
mask | ||
}); | ||
@@ -211,2 +221,163 @@ | ||
/** | ||
* `Struct` objects encapsulate the validation logic for a specific type of | ||
* values. Once constructed, you use the `assert`, `is` or `validate` helpers to | ||
* validate unknown input data against the struct. | ||
*/ | ||
class Struct { | ||
constructor(props) { | ||
const { | ||
type, | ||
schema, | ||
validator, | ||
refiner, | ||
coercer = value => value, | ||
entries = function* () {} | ||
} = props; | ||
this.type = type; | ||
this.schema = schema; | ||
this.entries = entries; | ||
this.coercer = coercer; | ||
if (validator) { | ||
this.validator = (value, context) => { | ||
const result = validator(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.validator = () => []; | ||
} | ||
if (refiner) { | ||
this.refiner = (value, context) => { | ||
const result = refiner(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.refiner = () => []; | ||
} | ||
} | ||
/** | ||
* Assert that a value passes the struct's validation, throwing if it doesn't. | ||
*/ | ||
assert(value) { | ||
return assert(value, this); | ||
} | ||
/** | ||
* Create a value with the struct's coercion logic, then validate it. | ||
*/ | ||
create(value) { | ||
return create(value, this); | ||
} | ||
/** | ||
* Check if a value passes the struct's validation. | ||
*/ | ||
is(value) { | ||
return is(value, this); | ||
} | ||
/** | ||
* Mask a value, coercing and validating it, but returning only the subset of | ||
* properties defined by the struct's schema. | ||
*/ | ||
mask(value) { | ||
return mask(value, this); | ||
} | ||
/** | ||
* Validate a value with the struct's validation logic, returning a tuple | ||
* representing the result. | ||
* | ||
* You may optionally pass `true` for the `withCoercion` argument to coerce | ||
* the value before attempting to validate it. If you do, the result will | ||
* contain the coerced result when successful. | ||
*/ | ||
validate(value, options = {}) { | ||
return validate(value, this, options); | ||
} | ||
} | ||
/** | ||
* Assert that a value passes a struct, throwing if it doesn't. | ||
*/ | ||
function assert(value, struct) { | ||
const result = validate(value, struct); | ||
if (result[0]) { | ||
throw result[0]; | ||
} | ||
} | ||
/** | ||
* Create a value with the coercion logic of struct and validate it. | ||
*/ | ||
function create(value, struct) { | ||
const result = validate(value, struct, { | ||
coerce: true | ||
}); | ||
if (result[0]) { | ||
throw result[0]; | ||
} else { | ||
return result[1]; | ||
} | ||
} | ||
/** | ||
* Mask a value, returning only the subset of properties defined by a struct. | ||
*/ | ||
function mask(value, struct) { | ||
const result = validate(value, struct, { | ||
coerce: true, | ||
mask: true | ||
}); | ||
if (result[0]) { | ||
throw result[0]; | ||
} else { | ||
return result[1]; | ||
} | ||
} | ||
/** | ||
* Check if a value passes a struct. | ||
*/ | ||
function is(value, struct) { | ||
const result = validate(value, struct); | ||
return !result[0]; | ||
} | ||
/** | ||
* Validate a value against a struct, returning an error if invalid, or the | ||
* value (with potential coercion) if valid. | ||
*/ | ||
function validate(value, struct, options = {}) { | ||
const tuples = run(value, struct, options); | ||
const tuple = shiftIterator(tuples); | ||
if (tuple[0]) { | ||
const error = new StructError(tuple[0], function* () { | ||
for (const t of tuples) { | ||
if (t[0]) { | ||
yield t[0]; | ||
} | ||
} | ||
}); | ||
return [error, undefined]; | ||
} else { | ||
const v = tuple[1]; | ||
return [undefined, v]; | ||
} | ||
} | ||
function assign(...Structs) { | ||
@@ -796,26 +967,2 @@ const schemas = Structs.map(s => s.schema); | ||
/** | ||
* Augment a struct to mask its input to only properties defined in the struct. | ||
* | ||
* Note: You must use `create(value, Struct)` on the value to have the coercion | ||
* take effect! Using simply `assert()` or `is()` will not use coercion. | ||
*/ | ||
function masked(struct) { | ||
return coerce(struct, unknown(), x => { | ||
if (typeof struct.schema !== 'object' || struct.schema == null || typeof x !== 'object' || x == null) { | ||
return x; | ||
} else { | ||
const ret = {}; | ||
for (const key in struct.schema) { | ||
if (key in x) { | ||
ret[key] = x[key]; | ||
} | ||
} | ||
return ret; | ||
} | ||
}); | ||
} | ||
/** | ||
* Augment a struct to trim string inputs. | ||
@@ -832,156 +979,2 @@ * | ||
/** | ||
* `Struct` objects encapsulate the validation logic for a specific type of | ||
* values. Once constructed, you use the `assert`, `is` or `validate` helpers to | ||
* validate unknown input data against the struct. | ||
*/ | ||
class Struct { | ||
constructor(props) { | ||
const { | ||
type, | ||
schema, | ||
validator, | ||
refiner, | ||
coercer = value => value, | ||
entries = function* () {} | ||
} = props; | ||
this.type = type; | ||
this.schema = schema; | ||
this.entries = entries; | ||
this.coercer = coercer; | ||
if (validator) { | ||
this.validator = (value, context) => { | ||
const result = validator(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.validator = () => []; | ||
} | ||
if (refiner) { | ||
this.refiner = (value, context) => { | ||
const result = refiner(value, context); | ||
return toFailures(result, context, this, value); | ||
}; | ||
} else { | ||
this.refiner = () => []; | ||
} | ||
} | ||
/** | ||
* Assert that a value passes the struct's validation, throwing if it doesn't. | ||
*/ | ||
assert(value) { | ||
return assert(value, this); | ||
} | ||
/** | ||
* Create a value with the struct's coercion logic, then validate it. | ||
*/ | ||
create(value) { | ||
return create(value, this); | ||
} | ||
/** | ||
* Check if a value passes the struct's validation. | ||
*/ | ||
is(value) { | ||
return is(value, this); | ||
} | ||
/** | ||
* Mask a value, coercing and validating it, but returning only the subset of | ||
* properties defined by the struct's schema. | ||
*/ | ||
mask(value) { | ||
return mask(value, this); | ||
} | ||
/** | ||
* Validate a value with the struct's validation logic, returning a tuple | ||
* representing the result. | ||
* | ||
* You may optionally pass `true` for the `withCoercion` argument to coerce | ||
* the value before attempting to validate it. If you do, the result will | ||
* contain the coerced result when successful. | ||
*/ | ||
validate(value, options = {}) { | ||
return validate(value, this, options); | ||
} | ||
} | ||
/** | ||
* Assert that a value passes a struct, throwing if it doesn't. | ||
*/ | ||
function assert(value, struct) { | ||
const result = validate(value, struct); | ||
if (result[0]) { | ||
throw result[0]; | ||
} | ||
} | ||
/** | ||
* Create a value with the coercion logic of struct and validate it. | ||
*/ | ||
function create(value, struct) { | ||
const result = validate(value, struct, { | ||
coerce: true | ||
}); | ||
if (result[0]) { | ||
throw result[0]; | ||
} else { | ||
return result[1]; | ||
} | ||
} | ||
/** | ||
* Mask a value, returning only the subset of properties defined by a struct. | ||
*/ | ||
function mask(value, struct) { | ||
const M = masked(struct); | ||
const ret = create(value, M); | ||
return ret; | ||
} | ||
/** | ||
* Check if a value passes a struct. | ||
*/ | ||
function is(value, struct) { | ||
const result = validate(value, struct); | ||
return !result[0]; | ||
} | ||
/** | ||
* Validate a value against a struct, returning an error if invalid, or the | ||
* value (with potential coercion) if valid. | ||
*/ | ||
function validate(value, struct, options = {}) { | ||
const tuples = run(value, struct, options); | ||
const tuple = shiftIterator(tuples); | ||
if (tuple[0]) { | ||
const error = new StructError(tuple[0], function* () { | ||
for (const t of tuples) { | ||
if (t[0]) { | ||
yield t[0]; | ||
} | ||
} | ||
}); | ||
return [error, undefined]; | ||
} else { | ||
const v = tuple[1]; | ||
return [undefined, v]; | ||
} | ||
} | ||
/** | ||
* Ensure that a string, array, map, or set is empty. | ||
@@ -1112,3 +1105,2 @@ */ | ||
exports.mask = mask; | ||
exports.masked = masked; | ||
exports.max = max; | ||
@@ -1115,0 +1107,0 @@ exports.min = min; |
@@ -214,2 +214,3 @@ /** | ||
coerce?: boolean; | ||
mask?: boolean; | ||
}): [ | ||
@@ -276,9 +277,2 @@ StructError, | ||
/** | ||
* Augment a struct to mask its input to only properties defined in the struct. | ||
* | ||
* Note: You must use `create(value, Struct)` on the value to have the coercion | ||
* take effect! Using simply `assert()` or `is()` will not use coercion. | ||
*/ | ||
declare function masked<T, S>(struct: Struct<T, S>): Struct<T, S>; | ||
/** | ||
* Augment a struct to trim string inputs. | ||
@@ -1243,2 +1237,2 @@ * | ||
declare function struct<T>(name: string, validator: Validator): Struct<T, null>; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, masked, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; | ||
export { Failure, StructError, Struct, assert, create, mask, is, validate, Context, Infer, Describe, Result, Coercer, Validator, Refiner, coerce, defaulted, trimmed, empty, max, min, pattern, size, refine, any, array, boolean, date, enums, func, instance, integer, intersection, literal, map, never, nullable, number, object, optional, record, regexp, set, string, tuple, type, union, unknown, assign, define, dynamic, lazy, omit, partial, pick, struct }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Superstruct={})}(this,(function(e){"use strict";class t extends TypeError{constructor(e,t){let n;const{message:r,...o}=e,{path:i}=e;super(0===i.length?r:"At path: "+i.join(".")+" -- "+r),Object.assign(this,o),this.name=this.constructor.name,this.failures=()=>{var r;return null!=(r=n)?r:n=[e,...t()]}}}function n(e){return"object"==typeof e&&null!=e}function r(e){if("[object Object]"!==Object.prototype.toString.call(e))return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype}function o(e){return"string"==typeof e?JSON.stringify(e):""+e}function i(e,t,n,r){if(!0===e)return;!1===e?e={}:"string"==typeof e&&(e={message:e});const{path:i,branch:c}=t,{type:a}=n,{refinement:s,message:u="Expected a value of type `"+a+"`"+(s?" with refinement `"+s+"`":"")+", but received: `"+o(r)+"`"}=e;return{value:r,type:a,refinement:s,key:i[i.length-1],path:i,branch:c,...e,message:u}}function*c(e,t,r,o){var c;n(c=e)&&"function"==typeof c[Symbol.iterator]||(e=[e]);for(const n of e){const e=i(n,t,r,o);e&&(yield e)}}function*a(e,t,r={}){const{path:o=[],branch:i=[e],coerce:c=!1}=r,s={path:o,branch:i};c&&(e=t.coercer(e,s));let u=!0;for(const n of t.validator(e,s))u=!1,yield[n,void 0];for(let[r,f,l]of t.entries(e,s)){const t=a(f,l,{path:void 0===r?o:[...o,r],branch:void 0===r?i:[...i,f],coerce:c});for(const o of t)o[0]?(u=!1,yield[o[0],void 0]):c&&(f=o[1],void 0===r?e=f:e instanceof Map?e.set(r,f):e instanceof Set?e.add(f):n(e)&&(e[r]=f))}if(u)for(const n of t.refiner(e,s))u=!1,yield[n,void 0];u&&(yield[void 0,e])}function s(e,t){return new v({type:e,schema:null,validator:t})}function u(){return s("never",(()=>!1))}function f(e){const t=e?Object.keys(e):[],r=u();return new v({type:"object",schema:e||null,*entries(o){if(e&&n(o)){const n=new Set(Object.keys(o));for(const r of t)n.delete(r),yield[r,o[r],e[r]];for(const e of n)yield[e,o[e],r]}},validator:e=>n(e)||"Expected an object, but received: "+o(e),coercer:e=>n(e)?{...e}:e})}function l(e){return new v({...e,validator:(t,n)=>void 0===t||e.validator(t,n),refiner:(t,n)=>void 0===t||e.refiner(t,n)})}function d(){return s("string",(e=>"string"==typeof e||"Expected a string, but received: "+o(e)))}function p(){return s("unknown",(()=>!0))}function y(e,t,n){return new v({...e,coercer:(r,o)=>x(r,t)?e.coercer(n(r,o),o):e.coercer(r,o)})}function h(e){return y(e,p(),(t=>{if("object"!=typeof e.schema||null==e.schema||"object"!=typeof t||null==t)return t;{const n={};for(const r in e.schema)r in t&&(n[r]=t[r]);return n}}))}class v{constructor(e){const{type:t,schema:n,validator:r,refiner:o,coercer:i=(e=>e),entries:a=function*(){}}=e;this.type=t,this.schema=n,this.entries=a,this.coercer=i,this.validator=r?(e,t)=>c(r(e,t),t,this,e):()=>[],this.refiner=o?(e,t)=>c(o(e,t),t,this,e):()=>[]}assert(e){return m(e,this)}create(e){return b(e,this)}is(e){return x(e,this)}mask(e){return g(e,this)}validate(e,t={}){return w(e,this,t)}}function m(e,t){const n=w(e,t);if(n[0])throw n[0]}function b(e,t){const n=w(e,t,{coerce:!0});if(n[0])throw n[0];return n[1]}function g(e,t){return b(e,h(t))}function x(e,t){return!w(e,t)[0]}function w(e,n,r={}){const o=a(e,n,r),i=function(e){const{done:t,value:n}=e.next();return t?void 0:n}(o);if(i[0]){return[new t(i[0],(function*(){for(const e of o)e[0]&&(yield e[0])})),void 0]}return[void 0,i[1]]}function j(e,t,n){return new v({...e,*refiner(r,o){yield*e.refiner(r,o);const i=c(n(r,o),o,e,r);for(const e of i)yield{...e,refinement:t}}})}e.Struct=v,e.StructError=t,e.any=function(){return s("any",(()=>!0))},e.array=function(e){return new v({type:"array",schema:e,*entries(t){if(e&&Array.isArray(t))for(const[n,r]of t.entries())yield[n,r,e]},coercer:e=>Array.isArray(e)?e.slice():e,validator:e=>Array.isArray(e)||"Expected an array value, but received: "+o(e)})},e.assert=m,e.assign=function(...e){const t=e.map((e=>e.schema));return f(Object.assign({},...t))},e.boolean=function(){return s("boolean",(e=>"boolean"==typeof e))},e.coerce=y,e.create=b,e.date=function(){return s("date",(e=>e instanceof Date&&!isNaN(e.getTime())||"Expected a valid `Date` object, but received: "+o(e)))},e.defaulted=function(e,t,n={}){return y(e,p(),(e=>{const o="function"==typeof t?t():t;if(void 0===e)return o;if(!n.strict&&r(e)&&r(o)){const t={...e};let n=!1;for(const e in o)void 0===t[e]&&(t[e]=o[e],n=!0);if(n)return t}return e}))},e.define=s,e.dynamic=function(e){return new v({type:"dynamic",schema:null,*entries(t,n){const r=e(t,n);yield*r.entries(t,n)},validator:(t,n)=>e(t,n).validator(t,n),coercer:(t,n)=>e(t,n).coercer(t,n)})},e.empty=function(e){const t="Expected an empty "+e.type;return j(e,"empty",(e=>{if(e instanceof Map||e instanceof Set){const{size:n}=e;return 0===n||t+" but received one with a size of `"+n+"`"}{const{length:n}=e;return 0===n||t+" but received one with a length of `"+n+"`"}}))},e.enums=function(e){const t={},n=e.map((e=>o(e))).join();for(const n of e)t[n]=n;return new v({type:"enums",schema:t,validator:t=>e.includes(t)||"Expected one of `"+n+"`, but received: "+o(t)})},e.func=function(){return s("func",(e=>"function"==typeof e||"Expected a function, but received: "+o(e)))},e.instance=function(e){return s("instance",(t=>t instanceof e||"Expected a `"+e.name+"` instance, but received: "+o(t)))},e.integer=function(){return s("integer",(e=>"number"==typeof e&&!isNaN(e)&&Number.isInteger(e)||"Expected an integer, but received: "+o(e)))},e.intersection=function(e){return new v({type:"intersection",schema:null,*entries(t,n){for(const r of e)yield*r.entries(t,n)},*validator(t,n){for(const r of e)yield*r.validator(t,n)},*refiner(t,n){for(const r of e)yield*r.refiner(t,n)}})},e.is=x,e.lazy=function(e){let t;return new v({type:"lazy",schema:null,*entries(n,r){null!=t||(t=e()),yield*t.entries(n,r)},validator:(n,r)=>(null!=t||(t=e()),t.validator(n,r)),coercer:(n,r)=>(null!=t||(t=e()),t.coercer(n,r))})},e.literal=function(e){const t=o(e);return s("literal",(n=>n===e||"Expected the literal `"+t+"`, but received: "+o(n)))},e.map=function(e,t){return new v({type:"map",schema:null,*entries(n){if(e&&t&&n instanceof Map)for(const[r,o]of n.entries())yield[r,r,e],yield[r,o,t]},coercer:e=>e instanceof Map?new Map(e):e,validator:e=>e instanceof Map||"Expected a `Map` object, but received: "+o(e)})},e.mask=g,e.masked=h,e.max=function(e,t,n={}){const{exclusive:r}=n;return j(e,"max",(n=>r?n<t:n<=t||"Expected a "+e.type+" greater than "+(r?"":"or equal to ")+t+" but received `"+n+"`"))},e.min=function(e,t,n={}){const{exclusive:r}=n;return j(e,"min",(n=>r?n>t:n>=t||"Expected a "+e.type+" greater than "+(r?"":"or equal to ")+t+" but received `"+n+"`"))},e.never=u,e.nullable=function(e){return new v({...e,validator:(t,n)=>null===t||e.validator(t,n),refiner:(t,n)=>null===t||e.refiner(t,n)})},e.number=function(){return s("number",(e=>"number"==typeof e&&!isNaN(e)||"Expected a number, but received: "+o(e)))},e.object=f,e.omit=function(e,t){const{schema:n}=e,r={...n};for(const e of t)delete r[e];return f(r)},e.optional=l,e.partial=function(e){const t=e instanceof v?{...e.schema}:{...e};for(const e in t)t[e]=l(t[e]);return f(t)},e.pattern=function(e,t){return j(e,"pattern",(n=>t.test(n)||"Expected a "+e.type+" matching `/"+t.source+'/` but received "'+n+'"'))},e.pick=function(e,t){const{schema:n}=e,r={};for(const e of t)r[e]=n[e];return f(r)},e.record=function(e,t){return new v({type:"record",schema:null,*entries(r){if(n(r))for(const n in r){const o=r[n];yield[n,n,e],yield[n,o,t]}},validator:e=>n(e)||"Expected an object, but received: "+o(e)})},e.refine=j,e.regexp=function(){return s("regexp",(e=>e instanceof RegExp))},e.set=function(e){return new v({type:"set",schema:null,*entries(t){if(e&&t instanceof Set)for(const n of t)yield[n,n,e]},coercer:e=>e instanceof Set?new Set(e):e,validator:e=>e instanceof Set||"Expected a `Set` object, but received: "+o(e)})},e.size=function(e,t,n=t){const r="Expected a "+e.type,o=t===n?"of `"+t+"`":"between `"+t+"` and `"+n+"`";return j(e,"size",(e=>{if("number"==typeof e||e instanceof Date)return t<=e&&e<=n||r+" "+o+" but received `"+e+"`";if(e instanceof Map||e instanceof Set){const{size:i}=e;return t<=i&&i<=n||r+" with a size "+o+" but received one with a size of `"+i+"`"}{const{length:i}=e;return t<=i&&i<=n||r+" with a length "+o+" but received one with a length of `"+i+"`"}}))},e.string=d,e.struct=function(e,t){return console.warn("superstruct@0.11 - The `struct` helper has been renamed to `define`."),s(e,t)},e.trimmed=function(e){return y(e,d(),(e=>e.trim()))},e.tuple=function(e){const t=u();return new v({type:"tuple",schema:null,*entries(n){if(Array.isArray(n)){const r=Math.max(e.length,n.length);for(let o=0;o<r;o++)yield[o,n[o],e[o]||t]}},validator:e=>Array.isArray(e)||"Expected an array, but received: "+o(e)})},e.type=function(e){const t=Object.keys(e);return new v({type:"type",schema:e,*entries(r){if(n(r))for(const n of t)yield[n,r[n],e[n]]},validator:e=>n(e)||"Expected an object, but received: "+o(e)})},e.union=function(e){const t=e.map((e=>e.type)).join(" | ");return new v({type:"union",schema:null,validator(n,r){const i=[];for(const t of e){const[...e]=a(n,t,r),[o]=e;if(!o[0])return[];for(const[t]of e)t&&i.push(t)}return["Expected the value to satisfy a union of `"+t+"`, but received: "+o(n),...i]}})},e.unknown=p,e.validate=w,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Superstruct={})}(this,(function(e){"use strict";class t extends TypeError{constructor(e,t){let n;const{message:r,...o}=e,{path:i}=e;super(0===i.length?r:"At path: "+i.join(".")+" -- "+r),Object.assign(this,o),this.name=this.constructor.name,this.failures=()=>{var r;return null!=(r=n)?r:n=[e,...t()]}}}function n(e){return"object"==typeof e&&null!=e}function r(e){if("[object Object]"!==Object.prototype.toString.call(e))return!1;const t=Object.getPrototypeOf(e);return null===t||t===Object.prototype}function o(e){return"string"==typeof e?JSON.stringify(e):""+e}function i(e,t,n,r){if(!0===e)return;!1===e?e={}:"string"==typeof e&&(e={message:e});const{path:i,branch:c}=t,{type:a}=n,{refinement:s,message:u="Expected a value of type `"+a+"`"+(s?" with refinement `"+s+"`":"")+", but received: `"+o(r)+"`"}=e;return{value:r,type:a,refinement:s,key:i[i.length-1],path:i,branch:c,...e,message:u}}function*c(e,t,r,o){var c;n(c=e)&&"function"==typeof c[Symbol.iterator]||(e=[e]);for(const n of e){const e=i(n,t,r,o);e&&(yield e)}}function*a(e,t,r={}){const{path:o=[],branch:i=[e],coerce:c=!1,mask:s=!1}=r,u={path:o,branch:i};if(c&&(e=t.coercer(e,u),s&&"type"!==t.type&&n(t.schema)&&n(e)&&!Array.isArray(e)))for(const n in e)void 0===t.schema[n]&&delete e[n];let f=!0;for(const n of t.validator(e,u))f=!1,yield[n,void 0];for(let[r,l,d]of t.entries(e,u)){const t=a(l,d,{path:void 0===r?o:[...o,r],branch:void 0===r?i:[...i,l],coerce:c,mask:s});for(const o of t)o[0]?(f=!1,yield[o[0],void 0]):c&&(l=o[1],void 0===r?e=l:e instanceof Map?e.set(r,l):e instanceof Set?e.add(l):n(e)&&(e[r]=l))}if(f)for(const n of t.refiner(e,u))f=!1,yield[n,void 0];f&&(yield[void 0,e])}class s{constructor(e){const{type:t,schema:n,validator:r,refiner:o,coercer:i=(e=>e),entries:a=function*(){}}=e;this.type=t,this.schema=n,this.entries=a,this.coercer=i,this.validator=r?(e,t)=>c(r(e,t),t,this,e):()=>[],this.refiner=o?(e,t)=>c(o(e,t),t,this,e):()=>[]}assert(e){return u(e,this)}create(e){return f(e,this)}is(e){return d(e,this)}mask(e){return l(e,this)}validate(e,t={}){return p(e,this,t)}}function u(e,t){const n=p(e,t);if(n[0])throw n[0]}function f(e,t){const n=p(e,t,{coerce:!0});if(n[0])throw n[0];return n[1]}function l(e,t){const n=p(e,t,{coerce:!0,mask:!0});if(n[0])throw n[0];return n[1]}function d(e,t){return!p(e,t)[0]}function p(e,n,r={}){const o=a(e,n,r),i=function(e){const{done:t,value:n}=e.next();return t?void 0:n}(o);if(i[0]){return[new t(i[0],(function*(){for(const e of o)e[0]&&(yield e[0])})),void 0]}return[void 0,i[1]]}function y(e,t){return new s({type:e,schema:null,validator:t})}function h(){return y("never",(()=>!1))}function v(e){const t=e?Object.keys(e):[],r=h();return new s({type:"object",schema:e||null,*entries(o){if(e&&n(o)){const n=new Set(Object.keys(o));for(const r of t)n.delete(r),yield[r,o[r],e[r]];for(const e of n)yield[e,o[e],r]}},validator:e=>n(e)||"Expected an object, but received: "+o(e),coercer:e=>n(e)?{...e}:e})}function m(e){return new s({...e,validator:(t,n)=>void 0===t||e.validator(t,n),refiner:(t,n)=>void 0===t||e.refiner(t,n)})}function b(){return y("string",(e=>"string"==typeof e||"Expected a string, but received: "+o(e)))}function g(){return y("unknown",(()=>!0))}function w(e,t,n){return new s({...e,coercer:(r,o)=>d(r,t)?e.coercer(n(r,o),o):e.coercer(r,o)})}function x(e,t,n){return new s({...e,*refiner(r,o){yield*e.refiner(r,o);const i=c(n(r,o),o,e,r);for(const e of i)yield{...e,refinement:t}}})}e.Struct=s,e.StructError=t,e.any=function(){return y("any",(()=>!0))},e.array=function(e){return new s({type:"array",schema:e,*entries(t){if(e&&Array.isArray(t))for(const[n,r]of t.entries())yield[n,r,e]},coercer:e=>Array.isArray(e)?e.slice():e,validator:e=>Array.isArray(e)||"Expected an array value, but received: "+o(e)})},e.assert=u,e.assign=function(...e){const t=e.map((e=>e.schema));return v(Object.assign({},...t))},e.boolean=function(){return y("boolean",(e=>"boolean"==typeof e))},e.coerce=w,e.create=f,e.date=function(){return y("date",(e=>e instanceof Date&&!isNaN(e.getTime())||"Expected a valid `Date` object, but received: "+o(e)))},e.defaulted=function(e,t,n={}){return w(e,g(),(e=>{const o="function"==typeof t?t():t;if(void 0===e)return o;if(!n.strict&&r(e)&&r(o)){const t={...e};let n=!1;for(const e in o)void 0===t[e]&&(t[e]=o[e],n=!0);if(n)return t}return e}))},e.define=y,e.dynamic=function(e){return new s({type:"dynamic",schema:null,*entries(t,n){const r=e(t,n);yield*r.entries(t,n)},validator:(t,n)=>e(t,n).validator(t,n),coercer:(t,n)=>e(t,n).coercer(t,n)})},e.empty=function(e){const t="Expected an empty "+e.type;return x(e,"empty",(e=>{if(e instanceof Map||e instanceof Set){const{size:n}=e;return 0===n||t+" but received one with a size of `"+n+"`"}{const{length:n}=e;return 0===n||t+" but received one with a length of `"+n+"`"}}))},e.enums=function(e){const t={},n=e.map((e=>o(e))).join();for(const n of e)t[n]=n;return new s({type:"enums",schema:t,validator:t=>e.includes(t)||"Expected one of `"+n+"`, but received: "+o(t)})},e.func=function(){return y("func",(e=>"function"==typeof e||"Expected a function, but received: "+o(e)))},e.instance=function(e){return y("instance",(t=>t instanceof e||"Expected a `"+e.name+"` instance, but received: "+o(t)))},e.integer=function(){return y("integer",(e=>"number"==typeof e&&!isNaN(e)&&Number.isInteger(e)||"Expected an integer, but received: "+o(e)))},e.intersection=function(e){return new s({type:"intersection",schema:null,*entries(t,n){for(const r of e)yield*r.entries(t,n)},*validator(t,n){for(const r of e)yield*r.validator(t,n)},*refiner(t,n){for(const r of e)yield*r.refiner(t,n)}})},e.is=d,e.lazy=function(e){let t;return new s({type:"lazy",schema:null,*entries(n,r){null!=t||(t=e()),yield*t.entries(n,r)},validator:(n,r)=>(null!=t||(t=e()),t.validator(n,r)),coercer:(n,r)=>(null!=t||(t=e()),t.coercer(n,r))})},e.literal=function(e){const t=o(e);return y("literal",(n=>n===e||"Expected the literal `"+t+"`, but received: "+o(n)))},e.map=function(e,t){return new s({type:"map",schema:null,*entries(n){if(e&&t&&n instanceof Map)for(const[r,o]of n.entries())yield[r,r,e],yield[r,o,t]},coercer:e=>e instanceof Map?new Map(e):e,validator:e=>e instanceof Map||"Expected a `Map` object, but received: "+o(e)})},e.mask=l,e.max=function(e,t,n={}){const{exclusive:r}=n;return x(e,"max",(n=>r?n<t:n<=t||"Expected a "+e.type+" greater than "+(r?"":"or equal to ")+t+" but received `"+n+"`"))},e.min=function(e,t,n={}){const{exclusive:r}=n;return x(e,"min",(n=>r?n>t:n>=t||"Expected a "+e.type+" greater than "+(r?"":"or equal to ")+t+" but received `"+n+"`"))},e.never=h,e.nullable=function(e){return new s({...e,validator:(t,n)=>null===t||e.validator(t,n),refiner:(t,n)=>null===t||e.refiner(t,n)})},e.number=function(){return y("number",(e=>"number"==typeof e&&!isNaN(e)||"Expected a number, but received: "+o(e)))},e.object=v,e.omit=function(e,t){const{schema:n}=e,r={...n};for(const e of t)delete r[e];return v(r)},e.optional=m,e.partial=function(e){const t=e instanceof s?{...e.schema}:{...e};for(const e in t)t[e]=m(t[e]);return v(t)},e.pattern=function(e,t){return x(e,"pattern",(n=>t.test(n)||"Expected a "+e.type+" matching `/"+t.source+'/` but received "'+n+'"'))},e.pick=function(e,t){const{schema:n}=e,r={};for(const e of t)r[e]=n[e];return v(r)},e.record=function(e,t){return new s({type:"record",schema:null,*entries(r){if(n(r))for(const n in r){const o=r[n];yield[n,n,e],yield[n,o,t]}},validator:e=>n(e)||"Expected an object, but received: "+o(e)})},e.refine=x,e.regexp=function(){return y("regexp",(e=>e instanceof RegExp))},e.set=function(e){return new s({type:"set",schema:null,*entries(t){if(e&&t instanceof Set)for(const n of t)yield[n,n,e]},coercer:e=>e instanceof Set?new Set(e):e,validator:e=>e instanceof Set||"Expected a `Set` object, but received: "+o(e)})},e.size=function(e,t,n=t){const r="Expected a "+e.type,o=t===n?"of `"+t+"`":"between `"+t+"` and `"+n+"`";return x(e,"size",(e=>{if("number"==typeof e||e instanceof Date)return t<=e&&e<=n||r+" "+o+" but received `"+e+"`";if(e instanceof Map||e instanceof Set){const{size:i}=e;return t<=i&&i<=n||r+" with a size "+o+" but received one with a size of `"+i+"`"}{const{length:i}=e;return t<=i&&i<=n||r+" with a length "+o+" but received one with a length of `"+i+"`"}}))},e.string=b,e.struct=function(e,t){return console.warn("superstruct@0.11 - The `struct` helper has been renamed to `define`."),y(e,t)},e.trimmed=function(e){return w(e,b(),(e=>e.trim()))},e.tuple=function(e){const t=h();return new s({type:"tuple",schema:null,*entries(n){if(Array.isArray(n)){const r=Math.max(e.length,n.length);for(let o=0;o<r;o++)yield[o,n[o],e[o]||t]}},validator:e=>Array.isArray(e)||"Expected an array, but received: "+o(e)})},e.type=function(e){const t=Object.keys(e);return new s({type:"type",schema:e,*entries(r){if(n(r))for(const n of t)yield[n,r[n],e[n]]},validator:e=>n(e)||"Expected an object, but received: "+o(e)})},e.union=function(e){const t=e.map((e=>e.type)).join(" | ");return new s({type:"union",schema:null,validator(n,r){const i=[];for(const t of e){const[...e]=a(n,t,r),[o]=e;if(!o[0])return[];for(const[t]of e)t&&i.push(t)}return["Expected the value to satisfy a union of `"+t+"`, but received: "+o(n),...i]}})},e.unknown=g,e.validate=p,Object.defineProperty(e,"__esModule",{value:!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
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
504188
9052