Skip to content

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

Class: OptionalSchema<T> ​

ts
class OptionalSchema {
  constructor(inner: T);
  readonly name: "optional";
  readonly inner: T;
  safeParseValue(input: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): Result<Infer<T> | undefined, SchemaError>;
  parseValue(value: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): Infer<T> | undefined;
  safeParseField(parent: SwXmlNode, key: string, ctx: SchemaParseContext, options?: SchemaParseOptions): Result<{ omitted: true } | { omitted: false; value: Infer<T>; source: "attribute" | "child" }, SchemaError>;
  serializeField(value: unknown): SchemaSerializeResult;
  optional(): OptionalSchema<T>;
}

Defined in: packages/xml/src/xml-schema/classes/optional.ts:20

A schema wrapper that accepts undefined values.

Missing fields are omitted from parsed objects, and undefined values are omitted during serialization.

Type Parameters ​

Type Parameter
T extends Schema<any>

Constructors ​

Constructor ​

ts
new OptionalSchema<T>(inner: T): OptionalSchema<T>;

Defined in: packages/xml/src/xml-schema/classes/optional.ts:23

Parameters ​

ParameterType
innerT

Returns ​

OptionalSchema<T>

Properties ​

name ​

ts
readonly name: "optional" = "optional";

Defined in: packages/xml/src/xml-schema/classes/optional.ts:21


inner ​

ts
readonly inner: T;

Defined in: packages/xml/src/xml-schema/classes/optional.ts:23

Methods ​

safeParseValue() ​

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

Defined in: packages/xml/src/xml-schema/classes/optional.ts:25

Parameters ​

ParameterType
inputSchemaInput
ctxSchemaParseContext
options?SchemaParseOptions

Returns ​

Result<Infer<T> | undefined, SchemaError>


parseValue() ​

ts
parseValue(
   value: SchemaInput, 
   ctx: SchemaParseContext, 
   options?: SchemaParseOptions): Infer<T> | undefined;

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

Parameters ​

ParameterType
valueSchemaInput
ctxSchemaParseContext
options?SchemaParseOptions

Returns ​

Infer<T> | undefined


safeParseField() ​

ts
safeParseField(
   parent: SwXmlNode, 
   key: string, 
   ctx: SchemaParseContext, 
   options?: SchemaParseOptions): Result<
  | {
  omitted: true;
}
  | {
  omitted: false;
  value: Infer<T>;
  source: "attribute" | "child";
}, SchemaError>;

Defined in: packages/xml/src/xml-schema/classes/optional.ts:45

Parameters ​

ParameterType
parentSwXmlNode
keystring
ctxSchemaParseContext
options?SchemaParseOptions

Returns ​

Result< | { omitted: true; } | { omitted: false; value: Infer<T>; source: "attribute" | "child"; }, SchemaError>


serializeField() ​

ts
serializeField(value: unknown): SchemaSerializeResult;

Defined in: packages/xml/src/xml-schema/classes/optional.ts:69

Parameters ​

ParameterType
valueunknown

Returns ​

SchemaSerializeResult


optional() ​

ts
optional(): OptionalSchema<T>;

Defined in: packages/xml/src/xml-schema/classes/optional.ts:77

Returns ​

OptionalSchema<T>