Documentation / @sw-file-lib/xml / xml-schema / ObjectSchema
Class: ObjectSchema<S, T>
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 Parameter | Default type |
|---|---|
S extends Shape | - |
T extends object | InferShape<S> |
Implements
Properties
name
readonly name: "object" = "object";Defined in: packages/xml/src/xml-schema/classes/object.ts:43
Implementation of
shape
readonly shape: S;Defined in: packages/xml/src/xml-schema/classes/object.ts:44
Methods
safeParseValue()
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
| Parameter | Type |
|---|---|
input | SchemaInput |
ctx | SchemaParseContext |
options? | SchemaParseOptions |
Returns
Result<T, SchemaError>
Implementation of
parseValue()
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
| Parameter | Type |
|---|---|
input | SchemaInput |
ctx | SchemaParseContext |
options? | SchemaParseOptions |
Returns
T
Throws
SchemaError when the value does not match the schema.
Implementation of
safeParseField()
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
| Parameter | Type |
|---|---|
parent | SwXmlNode |
key | string |
ctx | SchemaParseContext |
options? | SchemaParseOptions |
Returns
Implementation of
safeParse()
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
| Parameter | Type |
|---|---|
tree | | string | SwXmlNodeList | Uint8Array<ArrayBufferLike> |
rootTag | string |
options? | SchemaParseOptions |
Returns
Result<T, SchemaError>
Implementation of
parse()
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
| Parameter | Type |
|---|---|
tree | | string | SwXmlNodeList | Uint8Array<ArrayBufferLike> |
rootTag | string |
options? | SchemaParseOptions |
Returns
T
Throws
SchemaError when the value does not match the schema.
Implementation of
serializeField()
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
| Parameter | Type |
|---|---|
value | unknown |
Returns
Implementation of
safeSerialize()
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
| Parameter | Type |
|---|---|
data | Immutable<T> |
rootTag | string |
writer? | | XmlWriterOptions | XmlWriter |
Returns
Result<XmlWriter, SchemaSerializeError>
Implementation of
serialize()
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
| Parameter | Type |
|---|---|
data | Immutable<T> |
rootTag | string |
writer? | | XmlWriterOptions | XmlWriter |
Returns
Throws
SchemaSerializeError when any field cannot be serialized.
Implementation of
optional()
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
partial()
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()
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
| Parameter | Type |
|---|---|
shape | U | ((s: S) => U) |
Returns
ExtendObjectSchema<S, U>
Example
const withId = base.extend({ id: x.number() });
const withNestedZ = base.extend((s) => ({
position: s.position.extend({ z: x.number() }),
}));omit()
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
| Parameter | Type |
|---|---|
keys | U[] |
Returns
OmitObjectSchema<S, U>
create()
static create<S, T>(shape: S): ObjectSchema<S, T>;Defined in: packages/xml/src/xml-schema/classes/object.ts:50
Type Parameters
| Type Parameter | Default type |
|---|---|
S extends Shape | - |
T extends object | InferShape<S> |
Parameters
| Parameter | Type |
|---|---|
shape | S |
Returns
ObjectSchema<S, T>