Skip to content

Documentation / @sw-file-lib/xml / xml-schema / UnionSchema

Class: UnionSchema<T> ​

ts
class UnionSchema {
  constructor(schemas: T);
  readonly name: "union";
  readonly schemas: T;
  safeParseValue(input: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): Result<Infer<T[number]>, SchemaError>;
  parseValue(value: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): Infer<T[number]>;
  safeParseField(parent: SwXmlNode, key: string, ctx: SchemaParseContext, options?: SchemaParseOptions): SchemaParseFieldResult<Infer<T[number]>>;
  serializeField(value: unknown): SchemaSerializeResult;
  optional(): OptionalSchema<UnionSchema<T>>;
}

Defined in: packages/xml/src/xml-schema/classes/union.ts:34

A schema that tries multiple schemas from left to right.

Parsing succeeds with the first branch that matches. Parse errors keep the errors returned by each branch, and serialization errors report the direct branch schema names as the expected value.

Type Parameters ​

Type Parameter
T extends SchemaTuple

Implements ​

Constructors ​

Constructor ​

ts
new UnionSchema<T>(schemas: T): UnionSchema<T>;

Defined in: packages/xml/src/xml-schema/classes/union.ts:37

Parameters ​

ParameterType
schemasT

Returns ​

UnionSchema<T>

Properties ​

name ​

ts
readonly name: "union" = "union";

Defined in: packages/xml/src/xml-schema/classes/union.ts:35

Implementation of ​

Schema.name


schemas ​

ts
readonly schemas: T;

Defined in: packages/xml/src/xml-schema/classes/union.ts:37

Methods ​

safeParseValue() ​

ts
safeParseValue(
   input: SchemaInput, 
   ctx: SchemaParseContext, 
options?: SchemaParseOptions): Result<Infer<T[number]>, SchemaError>;

Defined in: packages/xml/src/xml-schema/classes/union.ts:39

Parses a Stormworks XML node or attribute value without throwing schema errors.

This is a low-level operation used by schema implementations. For parsing a full XML tree, prefer ElementSchema.safeParse.

Parameters ​

ParameterType
inputSchemaInput
ctxSchemaParseContext
options?SchemaParseOptions

Returns ​

Result<Infer<T[number]>, SchemaError>

Implementation of ​

Schema.safeParseValue


parseValue() ​

ts
parseValue(
   value: SchemaInput, 
   ctx: SchemaParseContext, 
options?: SchemaParseOptions): Infer<T[number]>;

Defined in: packages/xml/src/xml-schema/classes/union.ts:67

Parses a Stormworks XML node or attribute value.

This is a low-level operation used by schema implementations. For parsing a full XML tree, prefer ElementSchema.parse.

Parameters ​

ParameterType
valueSchemaInput
ctxSchemaParseContext
options?SchemaParseOptions

Returns ​

Infer<T[number]>

Throws ​

SchemaError when the value does not match the schema.

Implementation of ​

Schema.parseValue


safeParseField() ​

ts
safeParseField(
   parent: SwXmlNode, 
   key: string, 
   ctx: SchemaParseContext, 
options?: SchemaParseOptions): SchemaParseFieldResult<Infer<T[number]>>;

Defined in: packages/xml/src/xml-schema/classes/union.ts:75

Parses one field from an XML record node.

Primitive schemas read attributes. Element schemas read child elements.

This is primarily intended for schema implementations. Most callers should use ElementSchema.safeParse on a top-level element schema instead.

Parameters ​

ParameterType
parentSwXmlNode
keystring
ctxSchemaParseContext
options?SchemaParseOptions

Returns ​

SchemaParseFieldResult<Infer<T[number]>>

Implementation of ​

Schema.safeParseField


serializeField() ​

ts
serializeField(value: unknown): SchemaSerializeResult;

Defined in: packages/xml/src/xml-schema/classes/union.ts:104

Serializes one field without throwing.

This is primarily intended for schema implementations. Most callers should use ElementSchema.safeSerialize or ElementSchema.serialize on a top-level element schema instead.

Parameters ​

ParameterType
valueunknown

Returns ​

SchemaSerializeResult

Implementation of ​

Schema.serializeField


optional() ​

ts
optional(): OptionalSchema<UnionSchema<T>>;

Defined in: packages/xml/src/xml-schema/classes/union.ts:130

Returns a schema that accepts undefined values.

Returns ​

OptionalSchema<UnionSchema<T>>

Implementation of ​

Schema.optional