Skip to content

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

Class: ObjectSchema<S, T> ​

ts
class ObjectSchema {
  readonly name: "object";
  readonly shape: 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<ObjectSchema<S, T>>;
  partial(): ObjectSchema<PartialShape<S>, Partial<T>>;
  extend(shape: U | ((s: S) => U)): ExtendObjectSchema<S, U>;
  omit(keys: U[]): OmitObjectSchema<S, U>;
  create(shape: S): ObjectSchema<S, T>;
}

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

A schema that parses XML record elements as JavaScript objects.

Type Parameters ​

Type ParameterDefault type
S extends Shape-
T extends objectInferShape<S>

Implements ​

Properties ​

name ​

ts
readonly name: "object" = "object";

Defined in: packages/xml/src/xml-schema/classes/object.ts:43

Implementation of ​

ElementSchema.name


shape ​

ts
readonly shape: S;

Defined in: packages/xml/src/xml-schema/classes/object.ts:44

Methods ​

safeParseValue() ​

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

Defined in: packages/xml/src/xml-schema/classes/object.ts:60

Parses an XML element as a record without throwing an error.

Known fields are parsed with the configured field schemas. Primitive fields read attributes, while element schemas read child elements.

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/object.ts:96

Parses an XML element as a record.

Known fields are parsed with the configured field schemas. Primitive fields read attributes, while object and list fields read child elements.

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/object.ts:100

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/object.ts:109

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/object.ts:117

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/object.ts:125

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/object.ts:169

Serializes an object 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/object.ts:182

Serializes an object value into an XML element.

Parameters ​

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

Returns ​

XmlWriter

Throws ​

SchemaSerializeError when any field cannot be serialized.

Implementation of ​

ElementSchema.serialize


optional() ​

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

Defined in: packages/xml/src/xml-schema/classes/object.ts:186

Returns a schema that accepts undefined values.

Returns ​

OptionalSchema<ObjectSchema<S, T>>

Implementation of ​

ElementSchema.optional


partial() ​

ts
partial(): ObjectSchema<PartialShape<S>, Partial<T>>;

Defined in: packages/xml/src/xml-schema/classes/object.ts:193

Returns an object schema where every field is optional.

Returns ​

ObjectSchema<PartialShape<S>, Partial<T>>


extend() ​

ts
extend<U>(shape: U | ((s: S) => U)): ExtendObjectSchema<S, U>;

Defined in: packages/xml/src/xml-schema/classes/object.ts:216

Returns a new object schema by adding new fields or overwriting existing fields.

Pass a shape object directly when the new fields do not depend on the existing shape. Pass a callback when you need to reference existing fields, such as extending a nested object schema.

Type Parameters ​

Type Parameter
U extends Shape

Parameters ​

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

Returns ​

ExtendObjectSchema<S, U>

Example ​

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

omit() ​

ts
omit<U>(keys: U[]): OmitObjectSchema<S, U>;

Defined in: packages/xml/src/xml-schema/classes/object.ts:227

Returns a new object schema with specified keys are omitted.

Type Parameters ​

Type Parameter
U extends string | number | symbol

Parameters ​

ParameterType
keysU[]

Returns ​

OmitObjectSchema<S, U>


create() ​

ts
static create<S, T>(shape: S): ObjectSchema<S, T>;

Defined in: packages/xml/src/xml-schema/classes/object.ts:50

Type Parameters ​

Type ParameterDefault type
S extends Shape-
T extends objectInferShape<S>

Parameters ​

ParameterType
shapeS

Returns ​

ObjectSchema<S, T>