Skip to content

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

Class: ObjectRecordSchema<S1, S2, T1, T2> ​

ts
class ObjectRecordSchema extends RecordSchema<S2, T2> {
  constructor(itemSchema: S2);
  readonly name: "record";
  readonly itemSchema: S2;
  safeParseValue(input: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): Result<T2, SchemaError>;
  parseValue(input: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): T2;
  safeParseField(parent: SwXmlNode, key: string, ctx: SchemaParseContext, options?: SchemaParseOptions): SchemaParseFieldResult<T2>;
  safeParse(tree: string | SwXmlNodeList | Uint8Array<ArrayBufferLike>, rootTag: string, options?: SchemaParseOptions): Result<T2, SchemaError>;
  parse(tree: string | SwXmlNodeList | Uint8Array<ArrayBufferLike>, rootTag: string, options?: SchemaParseOptions): T2;
  serializeField(value: unknown): ElementSchemaSerializeResult;
  safeSerialize(data: Immutable<T2>, rootTag: string, writer?: XmlWriterOptions | XmlWriter): Result<XmlWriter, SchemaSerializeError>;
  serialize(data: Immutable<T2>, rootTag: string, writer?: XmlWriterOptions | XmlWriter): XmlWriter;
  extendItem(shape: U | ((s: S1) => U)): ObjectRecordSchema<ExtendShape<S1, U>, ExtendObjectSchema<S1, U>>;
  omitItem(keys: U[]): ObjectRecordSchema<Omit<S1, U>, OmitObjectSchema<S1, U>>;
  optional(): OptionalSchema<ObjectRecordSchema<S1, S2, T1, T2>>;
  create(itemSchema: S): RecordSchema<S, Record<string, Infer<S>>>;
  createWithObjectSchema(itemSchema: S2): ObjectRecordSchema<S1, S2, InferShape<S1>, Record<string, Infer<S2>>>;
}

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

A record schema whose item schema is an object schema.

Extends ​

Type Parameters ​

Type ParameterDefault type
S1 extends Shape-
S2 extends ObjectSchema<S1, T1>-
T1 extends objectInferShape<S1>
T2 extends Record<string, any>Record<string, Infer<S2>>

Constructors ​

Constructor ​

ts
protected new ObjectRecordSchema<S1, S2, T1, T2>(itemSchema: S2): ObjectRecordSchema<S1, S2, T1, T2>;

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

Parameters ​

ParameterType
itemSchemaS2

Returns ​

ObjectRecordSchema<S1, S2, T1, T2>

Inherited from ​

RecordSchema.constructor

Properties ​

name ​

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

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

Inherited from ​

RecordSchema.name


itemSchema ​

ts
readonly itemSchema: S2;

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

Inherited from ​

RecordSchema.itemSchema

Methods ​

safeParseValue() ​

ts
safeParseValue(
   input: SchemaInput, 
   ctx: SchemaParseContext, 
options?: SchemaParseOptions): Result<T2, 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<T2, SchemaError>

Inherited from ​

RecordSchema.safeParseValue


parseValue() ​

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

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 ​

T2

Throws ​

SchemaError when the value does not match the schema.

Inherited from ​

RecordSchema.parseValue


safeParseField() ​

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

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

Inherited from ​

RecordSchema.safeParseField


safeParse() ​

ts
safeParse(
   tree: 
  | string
  | SwXmlNodeList
  | Uint8Array<ArrayBufferLike>, 
   rootTag: string, 
options?: SchemaParseOptions): Result<T2, 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<T2, SchemaError>

Inherited from ​

RecordSchema.safeParse


parse() ​

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

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 ​

T2

Throws ​

SchemaError when the value does not match the schema.

Inherited from ​

RecordSchema.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

Inherited from ​

RecordSchema.serializeField


safeSerialize() ​

ts
safeSerialize(
   data: Immutable<T2>, 
   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<T2>
rootTagstring
writer?| XmlWriterOptions | XmlWriter

Returns ​

Result<XmlWriter, SchemaSerializeError>

Inherited from ​

RecordSchema.safeSerialize


serialize() ​

ts
serialize(
   data: Immutable<T2>, 
   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<T2>
rootTagstring
writer?| XmlWriterOptions | XmlWriter

Returns ​

XmlWriter

Throws ​

SchemaSerializeError when the value or any item cannot be serialized.

Inherited from ​

RecordSchema.serialize


extendItem() ​

ts
extendItem<U>(shape: U | ((s: S1) => U)): ObjectRecordSchema<ExtendShape<S1, U>, ExtendObjectSchema<S1, U>>;

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

Returns a new record schema by adding new fields or overwriting existing fields to the item schema.

Pass a shape object directly when the new item fields do not depend on the existing item shape. Pass a callback when you need to reference existing item fields.

Type Parameters ​

Type Parameter
U extends Shape

Parameters ​

ParameterType
shapeU | ((s: S1) => U)

Returns ​

ObjectRecordSchema<ExtendShape<S1, U>, ExtendObjectSchema<S1, U>>

Example ​

ts
const withId = items.extendItem({ id: x.number() });
const withNestedZ = items.extendItem((s) => ({
  position: s.position.extend({ z: x.number() }),
}));

omitItem() ​

ts
omitItem<U>(keys: U[]): ObjectRecordSchema<Omit<S1, U>, OmitObjectSchema<S1, U>>;

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

Returns a new record schema with specified keys are omitted from the item schema.

Type Parameters ​

Type Parameter
U extends string | number | symbol

Parameters ​

ParameterType
keysU[]

Returns ​

ObjectRecordSchema<Omit<S1, U>, OmitObjectSchema<S1, U>>


optional() ​

ts
optional(): OptionalSchema<ObjectRecordSchema<S1, S2, T1, T2>>;

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

Returns an optional version of this record schema.

Returns ​

OptionalSchema<ObjectRecordSchema<S1, S2, T1, T2>>

Overrides ​

RecordSchema.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>>>

Inherited from ​

RecordSchema.create


createWithObjectSchema() ​

ts
static createWithObjectSchema<S1, S2>(itemSchema: S2): ObjectRecordSchema<S1, S2, InferShape<S1>, Record<string, Infer<S2>>>;

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

Type Parameters ​

Type Parameter
S1 extends Shape
S2 extends ObjectSchema<S1, InferShape<S1>>

Parameters ​

ParameterType
itemSchemaS2

Returns ​

ObjectRecordSchema<S1, S2, InferShape<S1>, Record<string, Infer<S2>>>