xml-model 2.0.0-beta.5 → 2.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { DATA, isModel, model } from "./model.js";
2
2
  import XML, { ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode } from "./xml/xml-js.js";
3
3
  import { xml } from "./xml/schema-meta.js";
4
- import { normalizeCodecOptions, registerDefault } from "./xml/codec.js";
4
+ import { XMLCodecError, normalizeCodecOptions, registerDefault } from "./xml/codec.js";
5
5
  import { XMLBase, XMLBaseWithSource, xmlModel } from "./xml/model.js";
6
6
  import "./xml/index.js";
7
- export { DATA, XML, XMLBase, XMLBaseWithSource, ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode, isModel, model, normalizeCodecOptions, registerDefault, xml, xmlModel };
7
+ export { DATA, XML, XMLBase, XMLBaseWithSource, XMLCodecError, ZXMLCommentNode, ZXMLElementNode, ZXMLNode, ZXMLRoot, ZXMLTextNode, isModel, model, normalizeCodecOptions, registerDefault, xml, xmlModel };
@@ -1,5 +1,10 @@
1
1
  import { z } from 'zod';
2
2
  import { XMLElement } from './xml-js';
3
+ export declare class XMLCodecError extends Error {
4
+ readonly path: readonly (string | number)[];
5
+ readonly rawMessage: string;
6
+ constructor(rawMessage: string, path?: readonly (string | number)[], options?: ErrorOptions);
7
+ }
3
8
  export declare function assertSingleElement(xml: XMLElement[]): asserts xml is [XMLElement];
4
9
  export declare function assertSingleRoot(xml: XMLElement[]): asserts xml is [XMLElement & {
5
10
  elements: XMLElement[];
package/dist/xml/codec.js CHANGED
@@ -4,6 +4,21 @@ import { getUserOptions, prop } from "./schema-meta.js";
4
4
  import { kebabCase } from "../util/kebab-case.js";
5
5
  import { z } from "zod";
6
6
  //#region src/xml/codec.ts
7
+ var XMLCodecError = class extends Error {
8
+ path;
9
+ rawMessage;
10
+ constructor(rawMessage, path = [], options) {
11
+ super(path.length ? `[${path.join(".")}] ${rawMessage}` : rawMessage, options);
12
+ this.name = "XMLCodecError";
13
+ this.path = path;
14
+ this.rawMessage = rawMessage;
15
+ }
16
+ };
17
+ function rethrow(e, segment) {
18
+ const cause = e instanceof XMLCodecError ? e.cause : e;
19
+ const path = e instanceof XMLCodecError ? [segment, ...e.path] : [segment];
20
+ throw new XMLCodecError(e instanceof XMLCodecError ? e.rawMessage : e instanceof Error ? e.message : String(e), path, { cause });
21
+ }
7
22
  function assertSingleElement(xml) {
8
23
  if (xml.length !== 1) throw new Error(`Expected single XML element, got ${xml.length}`);
9
24
  }
@@ -206,7 +221,10 @@ registerDefault((schema) => {
206
221
  });
207
222
  },
208
223
  encode(ctx) {
209
- const data = outSchema.encode(ctx.data);
224
+ const data = schema.def.reverseTransform ? schema.def.reverseTransform(ctx.data, {
225
+ value: ctx.data,
226
+ issues: []
227
+ }) : outSchema.encode(ctx.data);
210
228
  const innerOpts = ctx.options.tagname !== inputCodecOptions.tagname ? {
211
229
  ...inputCodecOptions,
212
230
  tagname: ctx.options.tagname
@@ -311,12 +329,16 @@ registerDefault((schema) => {
311
329
  propCtx.xml = matches[0];
312
330
  }
313
331
  }
314
- o.decodeAsProperty({
315
- options: ctx.options,
316
- xml: ctx.xml,
317
- property: propCtx,
318
- result
319
- });
332
+ try {
333
+ o.decodeAsProperty({
334
+ options: ctx.options,
335
+ xml: ctx.xml,
336
+ property: propCtx,
337
+ result
338
+ });
339
+ } catch (e) {
340
+ rethrow(e, prop);
341
+ }
320
342
  }
321
343
  return result;
322
344
  },
@@ -332,20 +354,24 @@ registerDefault((schema) => {
332
354
  for (const item of sequence) if (typeof item === "string") {
333
355
  const o = options[item];
334
356
  if (!o) throw new Error(`Failed to resolve property options for sequence item ${item}`);
335
- o.encodeAsProperty({
336
- options: ctx.options,
337
- data,
338
- property: {
339
- name: item,
340
- options: o,
341
- tagname: o.propertyTagname({
357
+ try {
358
+ o.encodeAsProperty({
359
+ options: ctx.options,
360
+ data,
361
+ property: {
342
362
  name: item,
343
- options: o
344
- }),
345
- value: data[item]
346
- },
347
- result
348
- });
363
+ options: o,
364
+ tagname: o.propertyTagname({
365
+ name: item,
366
+ options: o
367
+ }),
368
+ value: data[item]
369
+ },
370
+ result
371
+ });
372
+ } catch (e) {
373
+ rethrow(e, item);
374
+ }
349
375
  } else result.elements.push(item);
350
376
  return result;
351
377
  }
@@ -353,6 +379,6 @@ registerDefault((schema) => {
353
379
  }
354
380
  });
355
381
  //#endregion
356
- export { XML_STATE_KEY, decode, encode, normalizeCodecOptions, registerDefault, xmlStateSchema };
382
+ export { XMLCodecError, XML_STATE_KEY, decode, encode, normalizeCodecOptions, registerDefault, xmlStateSchema };
357
383
 
358
384
  //# sourceMappingURL=codec.js.map
@@ -1,4 +1,17 @@
1
1
  import { z } from 'zod';
2
+ declare const Event_base: import('./model').XmlModelConstructor<z.ZodObject<{
3
+ title: z.ZodString;
4
+ publishedAt: z.ZodCodec<z.ZodString, z.ZodDate>;
5
+ }, z.core.$strip>, {
6
+ title: string;
7
+ publishedAt?: Date;
8
+ }>;
9
+ /**
10
+ * An event with a typed `Date` field stored as an ISO 8601 string in XML.
11
+ * Demonstrates using `z.codec` to transform a raw XML string into a native JS type.
12
+ */
13
+ export declare class Event extends Event_base {
14
+ }
2
15
  declare const Engine_base: import('./model').XmlModelConstructor<z.ZodObject<{
3
16
  type: z.ZodString;
4
17
  horsepower: z.ZodNumber;
@@ -1,6 +1,6 @@
1
1
  export * from './xml-js';
2
2
  export { xml } from './schema-meta';
3
3
  export type { UserCodecOptions, XMLState } from './codec';
4
- export { registerDefault, normalizeCodecOptions } from './codec';
4
+ export { registerDefault, normalizeCodecOptions, XMLCodecError } from './codec';
5
5
  export { xmlModel, XMLBase, XMLBaseWithSource, type XmlModelConstructor } from './model';
6
6
  //# sourceMappingURL=index.d.ts.map
package/dist/xml/model.js CHANGED
@@ -30,12 +30,7 @@ function xmlModel(schema, options) {
30
30
  static fromXML(input) {
31
31
  if (typeof input === "string") input = XML.parse(input);
32
32
  if (XML.isRoot(input)) input = XML.elementFromRoot(input);
33
- const schema = this.dataSchema;
34
- const inputData = decode(this.dataSchema, input);
35
- const xmlState = inputData[XML_STATE_KEY];
36
- const parsed = schema.parse(inputData);
37
- parsed[XML_STATE_KEY] = xmlState;
38
- return this.fromData(parsed);
33
+ return this.fromData(decode(this.dataSchema, input));
39
34
  }
40
35
  static toXML(instance) {
41
36
  const data = this.toData(instance);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-model",
3
- "version": "2.0.0-beta.5",
3
+ "version": "2.0.0-beta.6",
4
4
  "description": "allows transparent XML <-> Object conversion in typescript",
5
5
  "license": "MIT",
6
6
  "author": "MathisTLD",
@@ -53,4 +53,4 @@
53
53
  "vitest": "npm:@voidzero-dev/vite-plus-test@latest"
54
54
  },
55
55
  "packageManager": "npm@11.12.0"
56
- }
56
+ }