Skip to content

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

Class: RecordSchema<S, T> ​

ts
class RecordSchema {
  constructor(itemSchema: S);
  readonly name: "record";
  readonly itemSchema: S;
  safeParseValue(input: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): Result<T, SchemaError>;
  parseValue(input: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): T;
  safeParseField(parent: SwXmlNode, key: string, ctx: SchemaParseContext, options?: SchemaParseOptions): SchemaParseFieldResult<T>;
  safeParse(tree: string | SwXmlNodeList | Uint8Array<ArrayBufferLike>, rootTag: string, options?: SchemaParseOptions): Result<T, SchemaError>;
  parse(tree: string | SwXmlNodeList | Uint8Array<ArrayBufferLike>, rootTag: string, options?: SchemaParseOptions): T;
  serializeField(value: unknown): ElementSchemaSerializeResult;
  safeSerialize(data: Immutable<T>, rootTag: string, writer?: XmlWriterOptions | XmlWriter): Result<XmlWriter, SchemaSerializeError>;
  serialize(data: Immutable<T>, rootTag: string, writer?: XmlWriterOptions | XmlWriter): XmlWriter;
  optional(): OptionalSchema<RecordSchema<S, T>>;
  create(itemSchema: S): RecordSchema<S, Record<string, Infer<S>>>;
}

Defined in: packages/xml/src/xml-schema/classes/record.ts:53

A schema that parses XML children with arbitrary unique tags as a string-keyed object.

Extended by ​

Type Parameters ​

Type ParameterDefault type
S extends ElementSchema<any>-
T extends Record<string, any>Record<string, Infer<S>>

Implements ​

Constructors ​

Constructor ​

ts
protected new RecordSchema<S, T>(itemSchema: S): RecordSchema<S, T>;

Defined in: packages/xml/src/xml-schema/classes/record.ts:59

Parameters ​

ParameterType
itemSchemaS

Returns ​

RecordSchema<S, T>

Properties ​

name ​

ts
readonly name: "record" = "record";

Defined in: packages/xml/src/xml-schema/classes/record.ts:57

Implementation of ​

ElementSchema.name


itemSchema ​

ts
readonly itemSchema: S;

Defined in: packages/xml/src/xml-schema/classes/record.ts:59

Methods ​

safeParseValue() ​

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

Defined in: packages/xml/src/xml-schema/classes/record.ts:65

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<T, SchemaError>

Implementation of ​

ElementSchema.safeParseValue


parseValue() ​

ts
parseValue(
   input: SchemaInput, 
   ctx: SchemaParseContext, 
   options?: SchemaParseOptions): T;

Defined in: packages/xml/src/xml-schema/classes/record.ts:118

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
inputSchemaInput
ctxSchemaParseContext
options?SchemaParseOptions

Returns ​

T

Throws ​

SchemaError when the value does not match the schema.

Implementation of ​

ElementSchema.parseValue


safeParseField() ​

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

Defined in: packages/xml/src/xml-schema/classes/record.ts:122

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<T>

Implementation of ​

ElementSchema.safeParseField


safeParse() ​

ts
safeParse(
   tree: 
  | string
  | SwXmlNodeList
  | Uint8Array<ArrayBufferLike>, 
   rootTag: string, 
options?: SchemaParseOptions): Result<T, SchemaError>;

Defined in: packages/xml/src/xml-schema/classes/record.ts:131

Parses a complete XML tree without throwing schema errors.

The root element must have the supplied tag name.

Parameters ​

ParameterType
tree| string | SwXmlNodeList | Uint8Array<ArrayBufferLike>
rootTagstring
options?SchemaParseOptions

Returns ​

Result<T, SchemaError>

Implementation of ​

ElementSchema.safeParse


parse() ​

ts
parse(
   tree: 
  | string
  | SwXmlNodeList
  | Uint8Array<ArrayBufferLike>, 
   rootTag: string, 
   options?: SchemaParseOptions): T;

Defined in: packages/xml/src/xml-schema/classes/record.ts:139

Parses a complete XML tree.

The root element must have the supplied tag name.

Parameters ​

ParameterType
tree| string | SwXmlNodeList | Uint8Array<ArrayBufferLike>
rootTagstring
options?SchemaParseOptions

Returns ​

T

Throws ​

SchemaError when the value does not match the schema.

Implementation of ​

ElementSchema.parse


serializeField() ​

ts
serializeField(value: unknown): ElementSchemaSerializeResult;

Defined in: packages/xml/src/xml-schema/classes/record.ts:147

Serializes an element value without throwing.

This is primarily intended for schema implementations. Most callers should use safeSerialize or serialize instead.

Parameters ​

ParameterType
valueunknown

Returns ​

ElementSchemaSerializeResult

Implementation of ​

ElementSchema.serializeField


safeSerialize() ​

ts
safeSerialize(
   data: Immutable<T>, 
   rootTag: string, 
   writer?: 
  | XmlWriterOptions
| XmlWriter): Result<XmlWriter, SchemaSerializeError>;

Defined in: packages/xml/src/xml-schema/classes/record.ts:197

Serializes a record value into an XML element without throwing.

Parameters ​

ParameterType
dataImmutable<T>
rootTagstring
writer?| XmlWriterOptions | XmlWriter

Returns ​

Result<XmlWriter, SchemaSerializeError>

Implementation of ​

ElementSchema.safeSerialize


serialize() ​

ts
serialize(
   data: Immutable<T>, 
   rootTag: string, 
   writer?: 
  | XmlWriterOptions
  | XmlWriter): XmlWriter;

Defined in: packages/xml/src/xml-schema/classes/record.ts:211

Serializes a record value into an XML element.

Parameters ​

ParameterType
dataImmutable<T>
rootTagstring
writer?| XmlWriterOptions | XmlWriter

Returns ​

XmlWriter

Throws ​

SchemaSerializeError when the value or any item cannot be serialized.

Implementation of ​

ElementSchema.serialize


optional() ​

ts
optional(): OptionalSchema<RecordSchema<S, T>>;

Defined in: packages/xml/src/xml-schema/classes/record.ts:218

Returns an optional version of this record schema.

Returns ​

OptionalSchema<RecordSchema<S, T>>

Implementation of ​

ElementSchema.optional


create() ​

ts
static create<S>(itemSchema: S): RecordSchema<S, Record<string, Infer<S>>>;

Defined in: packages/xml/src/xml-schema/classes/record.ts:61

Type Parameters ​

Type Parameter
S extends ElementSchema<any>

Parameters ​

ParameterType
itemSchemaS

Returns ​

RecordSchema<S, Record<string, Infer<S>>>