struct.parse
Parses an unknown value with a struct and returns an error-first tuple.
@defjs/core · Function
Signature
function parse< S extends StructLike<unknown, unknown, boolean>, >( struct: S, value: unknown, options?: { aliases?: boolean; errorMap?: ErrorMap }, ): ParseResult<S["_struct"]["output"]>;
-
struct is the struct to parse with. A value that is not a struct throws
TypeError. -
value is the unknown input.
-
options.aliases defaults to
false: object keys are read by their logical names (displayName). Withtruethey are read by their wire names (display_name), the way HTTP/JSON codecs read them. The output always uses logical names. -
options.errorMap is an ErrorMap that rewrites issue messages for this call only, including bigint/date conversion failures from a struct created by another Core copy. Any string, including
'', replaces the message; onlyundefinedkeeps the default. Custom text is used verbatim. -
Parsing is fail-fast: it stops at the first failure, so the error holds one issue. Unknown object keys are stripped from the output, and the input is not modified.
-
At most 256 nested containers (
object,array,record, ortuple) are visited along a path, counting the root container as the first. The next container fails withinvalid_typeat its logical path, withexpected: 'value nested at most 256 levels deep'. In a union, later candidates may still succeed; if none match, the union reportsinvalid_union. Cycles through declared container fields also reach this limit; shared non-cyclic children are allowed. Union and intersection branches do not add a container level, andany/unknownvalues remain opaque rather than being traversed.
import { struct } from "@defjs/core";
const User = struct.object({ id: struct.number(), displayName: struct.string() });
function readUser() {
const [error, user] = struct.parse(User, { id: 1, displayName: "Ada" });
if (error) {
console.error(error.prettify());
return;
}
console.log(user.displayName);
}
readUser();any and unknown do not traverse their values, but an intersection may recursively merge two distinct object or array values returned by those leaves. This merge shares the same 256-container budget with its enclosing logical path. Exceeding it produces invalid_intersection at the rejected container's logical path. Identical references are retained without traversal, including a shared cyclic value.
Parameters
Prop
Type
struct Svalue unknownoptions? { aliases?: boolean; errorMap?: ErrorMap; }Returns
[null, output] or [StructError, undefined]. Validation failures do not throw. Exceptions from caller-provided getters and errorMap callbacks propagate unchanged.