Skip to content

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

Class: NumberSchema ​

ts
class NumberSchema {
  constructor();
  readonly name: "number";
  safeParseValue(input: SchemaInput, _ctx: SchemaParseContext, options?: SchemaParseOptions): Result<number, SchemaError>;
  parseValue(input: SchemaInput, ctx: SchemaParseContext, options?: SchemaParseOptions): number;
  safeParseField(parent: SwXmlNode, key: string, ctx: SchemaParseContext, options?: SchemaParseOptions): SchemaParseFieldAttributeResult<number>;
  serializeField(value: unknown): SchemaSerializeResult;
  optional(): OptionalSchema<NumberSchema>;
}

Defined in: packages/xml/src/xml-schema/classes/number.ts:26

A schema that parses XML attribute values as numbers.

Values are parsed with JavaScript's Number conversion. NaN is rejected unless SchemaParseOptions.allowNaN is enabled.

Implements ​

Constructors ​

Constructor ​

ts
new NumberSchema(): NumberSchema;

Returns ​

NumberSchema

Properties ​

name ​

ts
readonly name: "number" = "number";

Defined in: packages/xml/src/xml-schema/classes/number.ts:27

Implementation of ​

Schema.name

Methods ​

safeParseValue() ​

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

Defined in: packages/xml/src/xml-schema/classes/number.ts:29

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

Implementation of ​

Schema.safeParseValue


parseValue() ​

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

Defined in: packages/xml/src/xml-schema/classes/number.ts:56

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 ​

number

Throws ​

SchemaError when the value does not match the schema.

Implementation of ​

Schema.parseValue


safeParseField() ​

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

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

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 ​

SchemaParseFieldAttributeResult<number>

Implementation of ​

Schema.safeParseField


serializeField() ​

ts
serializeField(value: unknown): SchemaSerializeResult;

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

Serializes one field without throwing.

This is primarily intended for schema implementations. Most callers should use ElementSchema.safeSerialize or ElementSchema.serialize on a top-level element schema instead.

Parameters ​

ParameterType
valueunknown

Returns ​

SchemaSerializeResult

Implementation of ​

Schema.serializeField


optional() ​

ts
optional(): OptionalSchema<NumberSchema>;

Defined in: packages/xml/src/xml-schema/classes/number.ts:85

Returns a schema that accepts undefined values.

Returns ​

OptionalSchema<NumberSchema>

Implementation of ​

Schema.optional