All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
- Add
exactOptionalsupport intypestruct (#38)
- Fix
ExactOptionalizechangingnevertoundefined(#35)
- Add
exactOptionalstruct (#32)- This enables adding strictly optional fields to
objectstructs when paired with TypeScript'sexactOptionalPropertyTypes.
- This enables adding strictly optional fields to
- Newly exports the following types:
Assign,If,IsUnion,ObjectType,PartialObjectSchema,StructSchema,TupleSchema(#25)
- Newly exports the following types:
AnyStruct,EnumSchema,InferStructTuple,IsExactMatch,IsMatch,IsRecord,IsTuple,ObjectSchema,OmitBy,Optionalize,PickBy,Simplify,UnionToIntersection(#18).
- BREAKING: Expose separate build entry points and type declarations for CommonJS and ESM via package manifest
exports. (#18)
- Prepare package for releasing under
@metamaskorganisation (#1, #3, #4, #5, #6, #7, #8, #12, #15)- The package name is now
@metamask/superstruct. - The changelog file has been renamed to
CHANGELOG.md, and the format has changed to fit MetaMask's changelog style.
- The package name is now
- BREAKING: Expose CommonJS and ESM builds via package manifest
exports(#5)- It's no longer possible to import files from the
distfolder directly.
- It's no longer possible to import files from the
- Add an optional
messageargument to override error messages.- You can now pass in a
messageargument to all of the error checking functions which will override any error message with your own message. If you do, Superstruct's original descriptive message will still be accessible viaerror.cause. - For example:
assert(data, User, "The user is invalid!"); // StructError: The user is invalid!
- You can now pass in a
- BREAKING: Refinement functions are now called with valid, but potentially
unrefined values.
- Previously the functions passed in to
refinewould always be called with sub-elements (e.g., when using objects or arrays) that were completely valid and refined. However, this prevented collecting all the refinement errors from subelements in a validation in one go, which is common when validating forms. Note: this should not have any effect on almost all use cases, so you're safe to upgrade.
- Previously the functions passed in to
- Unions can now be coerced.
- Previously unions created a barrier in coercion such that structs nested inside unions would not have their coercion logic triggered, but this has been fixed.
- Assigning preserves
typestructs.- Previously using the
assignhelper would implicitly converttypestructs intoobjectstructs which wasn't expected and confusing, but this has been fixed.
- Previously using the
- BREAKING: The
maskhelper 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.
- BREAKING: The
maskedcoercion has been removed.- This previously allowed you to mix in masking to a specific struct, but the
maskhelper is a more robust way to do this, and it doesn't force you to maintain two separate structs.
- This previously allowed you to mix in masking to a specific struct, but the
- Structs can now define an
entriesiterator for nested values.- Previously iterating through nested values was defined in a one-off manner
inside certain structs, but this led to non-uniform support. Now, any struct
can define an
entriesiterator that will cause nested values to be automatically coerced and validated.
- Previously iterating through nested values was defined in a one-off manner
inside certain structs, but this led to non-uniform support. Now, any struct
can define an
- Coercion receives
contextobjects and supports nested values.- Previously context objects were only passed to the validations and
refinements. But the same context is passed to coercions too so you can
implement more complex logic. And coercions are automatically applied to
nested values thanks to the addition of
entries.
- Previously context objects were only passed to the validations and
refinements. But the same context is passed to coercions too so you can
implement more complex logic. And coercions are automatically applied to
nested values thanks to the addition of
- Iteration logic has gotten simpler, and more performant.
- The addition of the
entrieslogic has enabled us to only ever iterate through a tree of values one time for coercion and validation, instead of once each. This should speed up most standard use cases.
- The addition of the
- BREAKING: The
ctx.fail()function has been removed.- Previously you'd use it to return more information about a failure inside a struct. Now you can simply return a partial failure object.
- BREAKING: The
ctx.check()function has been removed.- Previously you'd use it to validate nested objects in more complex struct
shapes. Now you can use the new
entriesproperty for this instead.
- Previously you'd use it to validate nested objects in more complex struct
shapes. Now you can use the new
- BREAKING: The
context.structandcontext.valueproperties have been removed.- These properties were previously available, but unnecessary since anywhere
you have the context object you will also know the
valueand the specific struct that is being validated. Keeping them around required extra unnecessary plumbing in the library that made composing structs much more difficult, so they were removed.
- These properties were previously available, but unnecessary since anywhere
you have the context object you will also know the
-
New
Describeutility type.-
This new utility lets you define a struct from an existing TypeScript type and ensure that the struct's validation matches it, otherwise TypeScript's compiler will error. For example:
type User = { id: number; name: string; }; const User: Describe<User> = object({ id: string(), // This mistake will fail to pass type checking! name: string(), });
-
-
BREAKING: The
coercehelper has changed to be more type-safe.-
Previously
coercefunctions were called withvalue: unknownbecause they ran before all validation. However, now they take a new second argument that is another struct to narrow the cases where coercions occurs. This means thevaluefor coercion will now be type-safe. -
For example:
// Previously const MyNumber = coerce(number(), (value) => { return typeof value === "string" ? parseFloat(value) : value; }); // Now const MyNumber = coerce(number(), string(), (value) => { return parseFloat(value); });
-
-
New
assign,pick, andomitobject utilities.-
These utilities make composing object structs together possible, which should make re-using structs in your codebase easier.
-
For example:
// Combine two structs with `assign`: const a = object({ id: number() }); const b = object({ name: string() }); const c = assign([a, b]); // Pick out specific properties with `pick`: const a2 = pick(c, ["id"]); // Omit specific properties with `omit`: const a3 = omit(c, ["name"]);
-
-
New
unknownstruct.- This is the same as the existing
anystruct, but it will ensure that in TypeScript the value is of the more restrictiveunknowntype, so it encourages better type safety. - For example:
const Shape = type({ id: number(), name: string(), other: unknown(), });
- This is the same as the existing
-
New
integer,regexp, andfuncstructs.- These are just simple additions for common use cases of ensuring a value is an integer, a regular expression object (not a string!), or a function.
- For example:
const Shape = type({ id: integer(), matches: regexp(), send: func(), });
-
New
max/minrefinements.- For refining
number(orinteger) ordatestructs to ensure they are greater than or less than a specific threshold. The third argument can indicate whether to make the threshold exclusive (instead of the default inclusive). - For example:
const Index = min(number(), 0); const PastOrPresent = max(date(), new Date()); const Past = max(date(), new Date(), { exclusive: true });
- For refining
-
Even more information on errors.
- Errors now expose the
error.refinementproperty when the failure originated in a refinement validation. And they also now have anerror.keyproperty which is the key for the failure in the case of complex values like arrays/objects. (Previously the key was retrievable by checkingerror.path, but this will make the 90% case easier.)
- Errors now expose the
-
BREAKING: The
coercehelper has been renamed tocreate.-
This will hopefully make it more clear that it's fully coercing and validating a value against a struct, throwing errors if the value was invalid. This has caused confusion for people who though it would just coerce the value and return the unvalidated-but-coerced version.
-
For example:
// Previously const user = coerce(data, User); // Now const user = create(data, User);
-
-
BREAKING: The
struct,refinementandcoercionfactories have been renamed.-
This renaming is purely for keeping things slightly cleaner and easier to understand. The new names are
define,refine, andcoerce. Separating them slightly from the noun-based names used for the types themselves. -
For example:
// Previously const Email = struct('email', isEmail) const Positive = refinement('positive', number(), n => n > 0) const Trimmed = coercion(string(), s => s.trim() // Now const Email = define('email', isEmail) const Positive = refine(number(), 'positive', n => n > 0) const Trimmed = coerce(string(), s => s.trim())
-
-
BREAKING: The
lengthrefinement has been renamed tosize.-
This is to match with the expansion of it's abilities from purely strings and arrays to also now include numbers, maps, and sets. In addition you can also omit the
maxargument to specify an exact size: -
For example:
// Previously const Name = length(string(), 1, 100); const MyArray = length(array(string()), 3, 3); // Now const Name = size(string(), 1, 100); const MyArray = size(array(string()), 3); const Id = size(integer(), 1, Infinity); const MySet = size(set(), 1, 9);
-
-
BREAKING: The
StructTypeinferring helper has been renamed toInfer.-
This just makes it slightly easier to read what's going on when you're inferring a type.
-
For example:
// Previously type User = StructType<typeof User>; // Now type User = Infer<typeof User>;
-
-
BREAKING: The
error.typeproperty has been standardized.-
Previously it was a human-readable description that sort of incorporated the schema. Now it is simple the plain lowercase name of the struct in question, making it something you can use programmatically when formatting errors.
-
For example:
// Previously "Array<string>"; "[string,number]"; "Map<string,number>"; // Now "array"; "tuple"; "map";
-
-
BREAKING: All types are created from factories.
-
Previously depending on whether the type was a complex type or a scalar type they'd be defined different. Complex types used factories, whereas scalars used strings. Now all types are exposed as factories.
-
For example:
// Previously const User = struct.object({ name: "string", age: "number", }); // Now const User = object({ name: string(), age: number(), });
-
-
BREAKING: Custom scalars are no longer pre-defined as strings.
-
Previously, you would define all of your "custom" types in a single place in your codebase and then refer to them in structs later on with a string value. This worked, but added a layer of unnecessary indirection, and made it impossible to accomodate runtime type signatures.
-
For example:
// Previously const struct = superstruct({ types: { email: isEmail, }, }); const Email = struct("email"); // Now const Email = struct("email", isEmail);
-
-
BREAKING: Coercion is now separate from validation.
- Previously there was native logic for handling default values for structs when validating them. This has been abstracted into the ability to define any custom coercion logic for structs, and it has been separate from validation to make it very clear when data can change and when it cannot.
- For example:
const output = User.assert(input); // Would now be: const input = coerce(input, User);
-
BREAKING: Validation context is now a dictionary of properties.
- Previously when performing complex validation logic that was dependent on
other properties on the root object, you could use the second
branchargument to the validation function. This argument has been changed to be acontextdictionary with more information. The same branch argument can now be accessed ascontext.branch, along with the new information.
- Previously when performing complex validation logic that was dependent on
other properties on the root object, you could use the second
-
BREAKING: Unknown properties of objects now have a
'never'type.- Previously unknown properties would throw errors with
type === null, however the newly introduced'never'type is now used instead.
- Previously unknown properties would throw errors with
-
BREAKING: Defaults are now defined with a separate coercion helper.
-
Previously all structs took a second argument that defined the default value to use if an
undefinedvalue was present. This has been pulled out into a separate helper now to clearly distinguish coercion logic. -
For example:
const Article = struct.object( { title: "string", }, { title: "Untitled", } ); // Now const Article = defaulted( object({ title: string(), }), { title: "Untitled", } );
-
-
BREAKING: Optional arguments are now defined with a separate factory.
-
Similarly to defaults, there is a new
optionalfactory for defined values that can also beundefined. -
For example:
const Flag = struct("string?"); // Now const Flag = optional(string());
-
-
BREAKING: Several structs have been renamed.
- This was necessary because structs are now exposed directly as variables, which
runs afoul of reserved words. So the following renames have been applied:
interface->typeenum->enumsfunction->func
- This was necessary because structs are now exposed directly as variables, which
runs afoul of reserved words. So the following renames have been applied:
- Superstruct is now written in TypeScript.
- It was rewritten from the ground up to make use of types, and to have better inline documented if you use a TypeScript-compatible IDE. There are probably improvements that can be made, so if you'd like to contribute please do!
- A new
partialstruct mimics TypeScript'sPartialutility.- The new struct validates that its input partially matches an object defined as a set of properties with associated types. All of the properties of the object are optional.
- A new
sizestruct allows validating array and string lengths.- The new struct validates that its input has a certain size, by checking its
lengthproperty. This works strings or arrays.
- The new struct validates that its input has a certain size, by checking its
- You can now provide a custom
Errorsetting.- By passing in your own constructor when configuring Superstruct you can have complete control over the exact errors that are generated by structs that fail validation.
- BREAKING: Several structs have been renamed.
- Superstruct tries to mimic established naming schemes whenever possible for its
API, and TypeScript is one of our main comparisons. To make things easier for
people, we've renamed a few structs to more closely match their TypeScript
counterparts:
- The
liststruct is now calledarray. - The
partialstruct is now calledpick. - The
dictstruct is now calledrecord.
- The
- Superstruct tries to mimic established naming schemes whenever possible for its
API, and TypeScript is one of our main comparisons. To make things easier for
people, we've renamed a few structs to more closely match their TypeScript
counterparts:
- BREAKING: The interface struct now returns the original, unaltered value!
- In an effort to make things more familiar, the
interfacestruct now always returns the object that it is called with when it passes validation. So if the object was a function, a function will be returned. This makes it match more closely with the idea of "structural typing" that TypeScript and other typing systems are based on. If you want the old behavior, use thepickstruct.
- In an effort to make things more familiar, the
- BREAKING: Computed values function signatures have changed!
- Previously a computed value would be called with a signature of
(value, root)in some cases and(value, parent)in others. This was confusing, and the cause for the inconsistency was complex. This logic has been simplified, and now computed values are called with(value, branch, path)in all cases. - For example:
struct.dynamic((value, branch, path) => { value === branch[branch.length - 1]; // you can get the value... const parent = branch[branch.length - 2]; // ...and the parent... const key = path[path.length - 1]; // ...and the key... value === parent[key]; const root = branch[0]; // ...and the root! });
- Previously a computed value would be called with a signature of
- BREAKING: The
error.errorsproperty has been renamederror.failures, and isn't cyclical.- It being cyclical caused lots of issues whenever an
StructErrorobject was attempted to be serialized. And theerrorsproperty was slightly confusing because the elements of the array weren't full error objects. The new structure is easier to understand and work with.
- It being cyclical caused lots of issues whenever an
- BREAKING: The
error.reasonproperty is no longer special-cased.- Previously you could return a "reason" string from validator functions and it
would be added to error objects. However, now you must return an error properties
object (with a
reasonproperty if you'd like), and all of the properties will be added to the error object. This makes Superstruct even more flexible as far as custom error details go.
- Previously you could return a "reason" string from validator functions and it
would be added to error objects. However, now you must return an error properties
object (with a
- The
typeproperty of structs have been rewritten to be more clear.- This is an implementation mostly, but the
struct.typestring which shows up in error messages have been tweaked to be slightly more clear exactly what type they are checking for.
- This is an implementation mostly, but the
- BREAKING: The
enumsstruct has been removed.- This was special-cased in the API previously, but you can get the exact same
behavior by creating it using the
arrayandenumstructs:struct.array(struct.enum(["red", "blue", "green"]));
- This was special-cased in the API previously, but you can get the exact same
behavior by creating it using the
- BREAKING: The
anystruct has been removed! (Not the scalar though.)- Previously
struct.any()was exposed that did the same thing asstruct(), allowing you to use shorthands for common structs. But this was confusingly named because it has nothing to do with the'any'scalar type. And since it was redundant it has been removed.
- Previously
- BREAKING: The build process now outputs ES5 code.
- Previously it was outputting ES6 code, which posed problems for some builders. This change shouldn't really affect anyone negatively, but it's being released as a breaking version just in case.
- BREAKING: Invalid
Dateobjects are now considered invalid.- Previously using the built-in
'date'validator would only check that the object was aDateinstance, and not that it was a valid one. This has been fixed, and although it is technically a breaking change, most everyone would have expected this behavior to begin with.
- Previously using the built-in
- Errors can now contain reason information.
- Validator functions can now return string instead of a boolean, denoting the reason a value was invalid. This can then be used to create more helpful error messages.
- BREAKING: Validators must now return
true,falseor an error reason string.- Previously any truthy value would be considered valid. Now you can provide more
information for the thrown errors by providing a string which will be attached as
error.reason. However, this means that truthy string values now equate to invalid, not valid.
- Previously any truthy value would be considered valid. Now you can provide more
information for the thrown errors by providing a string which will be attached as
- BREAKING: Property validators now receive
dataas their second argument.- Previously you only had access to the property
value, but now you also have access to the entire object'sdata.
- Previously you only had access to the property
- Added the
dict,enum,intersection,unionandtuplestructs.- These are all available as
struct.dict,struct.enum, etc.
- These are all available as
- BREAKING:
objectstructs are no longer optional-ish.- Previously object struct types would not throw if
undefinedwas passed and no properties were required. This was not only confusing, but complex to maintain. Now if you want an object struct to be optional, use thestruct.optional(...)helper.
- Previously object struct types would not throw if
- Removed the
Struct.defaultmethod.- If you need to get the default value, use the
Struct.validateorStruct.assertmethods' return value instead.
- If you need to get the default value, use the
- BREAKING: The
validatemethod now returns[ error, result ].- Previously it only had a single return value, which necessitated extra type checking to see if the value was an error or a result. Now you can just destructure the array to get either return value, for easier coding.
- Errors have been simplified, removing "codes".
- Previously there were multiple types of errors that were thrown and you could
differentiate between them with the
error.codeproperty. But the other properties of the error already let you infer the code, so having multiple types of errors made for a larger API surface without much benefit.
- Previously there were multiple types of errors that were thrown and you could
differentiate between them with the
- The basic case is now
Struct(data).- Previously you had to use
Struct.assert(data). Although theassertmethod (and others) are still there, the basic case is a bit terser and more similar to the struct-initializing APIs in other languages.
- Previously you had to use
- BREAKING: Structs are now functions again.
- They are built on the same underlying schema classes underneath though, since that
helps the code structure. But to allow for the
struct = Struct({ ... })syntax the structs themselves have changed to be function.
- They are built on the same underlying schema classes underneath though, since that
helps the code structure. But to allow for the
- BREAKING: Structs are now classes instead of functions.
- This is better in terms of the API being a bit less magic-y. It's also useful so
that we can add other helpful methods to structs besides the
assertmethod. What was previouslystruct(data)is nowstruct.assert(data).
- This is better in terms of the API being a bit less magic-y. It's also useful so
that we can add other helpful methods to structs besides the
- Initial release.