@endevops/peppol-schema 0.1.1-alpha.2 → 0.1.1-alpha.3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/refs/heads/master/src/schemas/json/package.json",
3
3
  "name": "@endevops/peppol-schema",
4
- "version": "0.1.1-alpha.2",
4
+ "version": "0.1.1-alpha.3",
5
5
  "description": "TypeScript Effect Schema models for PEPPOL BIS Billing 3.0 — decode, validate and encode Invoice, Credit Note, Invoice Response and Message Level Response XML, with generated code lists, Schematron rules and identifier validations",
6
6
  "keywords": [
7
7
  "bis-billing",
@@ -84,7 +84,6 @@ const encodeDocumentXml = Effect.fn('encode-peppol-document-xml')(function* (val
84
84
  content = yield* encodeInvoiceResponse(value as never);
85
85
  } else {
86
86
  const rootNodes = Object.keys(value);
87
- console.log(value, isPeppolInvoice(value), isPeppolCreditNote(value), isPeppolMessageLevelResponse(value), isPeppolInvoiceResponse(value));
88
87
  return yield* Effect.fail(
89
88
  new SchemaIssue.InvalidValue({ message: `Unsupported document type: ${value.profileId}\n${rootNodes.join(',')}` }, value, options)
90
89
  );
@@ -1,4 +1,4 @@
1
- import { Clock, Option, DateTime, Effect, Predicate, Schema, SchemaGetter, SchemaIssue, SchemaParser } from 'effect';
1
+ import { Clock, Option, DateTime, Effect, Predicate, Schema, SchemaGetter, SchemaIssue } from 'effect';
2
2
 
3
3
  import { formatXsdTime } from '#/schemas/utils/format-xsd-time.ts';
4
4
  import { opaque } from '#/schemas/utils/opaque.ts';
@@ -6,12 +6,13 @@ import { opaque } from '#/schemas/utils/opaque.ts';
6
6
  const timeRegex =
7
7
  /^(?<hours>[01]\d|2[0-3]):(?<minutes>[0-5]\d):(?<seconds>[0-5]\d)(?<nanoseconds>\.\d{1,9})?(?<tz>(?:Z|-0[1-9]|-1\d|-2[0-3]|-00:?(?:0[1-9]|[1-5]\d)|\+[01]\d|\+2[0-3])?(?:|:?[0-5]\d))$/;
8
8
  const timeZoneRegex = /^(?:(?<utc>Z)|[-+](?<hours>0[1-9]|1\d|2[0-3]|00):?(?<minutes>[0-5]\d))$/;
9
- const numberFromString = SchemaParser.decodeSync(
10
- Schema.UndefinedOr(Schema.String.pipe(Schema.withDecodingDefault(Effect.succeed('0')))).pipe(
11
- Schema.decodeTo(Schema.Number, { encode: SchemaGetter.String(), decode: SchemaGetter.Number() }),
12
- Schema.catchDecoding(() => Effect.succeedSome(0))
13
- )
14
- );
9
+ const numberFromString = (value: string | undefined): number => {
10
+ if (value === undefined) {
11
+ return 0;
12
+ }
13
+ const parsed = Number(value);
14
+ return Number.isFinite(parsed) ? parsed : 0;
15
+ };
15
16
 
16
17
  /**
17
18
  * @description Parse a timezone offset from a string. The string must be in the format `(+|-)HH:MM` where `HH` and `MM` are two digits.
@@ -1,9 +1,8 @@
1
- import { Schema } from 'effect';
1
+ import { DateTime } from 'effect';
2
2
 
3
3
  import type { PeppolDocument } from '#/schemas/peppol-document-schema.ts';
4
4
  import type { SchematronRule } from '#/schematron/helpers.ts';
5
5
 
6
- import { PeppolIsoDateString } from '#/schemas/peppol-iso-date-string.ts';
7
6
  import { getSupplierCountry, schematronRule } from '#/schematron/helpers.ts';
8
7
 
9
8
  const rule = {
@@ -13,7 +12,15 @@ const rule = {
13
12
  } as const satisfies SchematronRule;
14
13
 
15
14
  const DATE_REGEX = /^(0?[1-9]|[12][0-9]|3[01])[-/]?(0?[1-9]|1[0-2])[-/]?((?:19|20)[0-9]{2})$/;
16
- const encodeDateSync = Schema.encodeSync(PeppolIsoDateString);
15
+ const formatIssueDate = (value: unknown): string => {
16
+ if (typeof value === 'string') {
17
+ return value;
18
+ }
19
+ if (DateTime.isDateTime(value)) {
20
+ return DateTime.formatIsoDate(value);
21
+ }
22
+ return String(value);
23
+ };
17
24
 
18
25
  function evaluateGrR001_3(document: PeppolDocument): boolean {
19
26
  if (getSupplierCountry(document) !== 'GR' && getSupplierCountry(document) !== 'EL') {
@@ -29,7 +36,7 @@ function evaluateGrR001_3(document: PeppolDocument): boolean {
29
36
  return false;
30
37
  }
31
38
  const [, day, month, year] = dateMatch;
32
- const issueDate = encodeDateSync(document.issueDate);
39
+ const issueDate = formatIssueDate(document.issueDate);
33
40
  const issueDateSegments = issueDate.split('-');
34
41
  return day === issueDateSegments[2] && month === issueDateSegments[1] && year === issueDateSegments[0];
35
42
  }
@@ -1,4 +1,4 @@
1
- import { Predicate, Schema } from 'effect';
1
+ import { DateTime, Predicate, Schema } from 'effect';
2
2
 
3
3
  import type { PeppolDocument } from '#/schemas/peppol-document-schema.ts';
4
4
  import type { SchematronRule } from '#/schematron/helpers.ts';
@@ -12,7 +12,12 @@ const DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;
12
12
 
13
13
  const isValidDate = (value: string | undefined): boolean => value === undefined || (DATE_REGEX.test(value) && !Number.isNaN(Date.parse(value)));
14
14
 
15
- const encodeDateSync = Schema.encodeSync(Schema.Union([PeppolIsoDateString, Schema.String]));
15
+ const formatDate = (value: PeppolIsoDateString | string): string => {
16
+ if (typeof value === 'string') {
17
+ return value;
18
+ }
19
+ return DateTime.formatIsoDate(value);
20
+ };
16
21
  const isDate = Schema.is(PeppolIsoDateString);
17
22
 
18
23
  const collectDates = (document: PeppolDocument): Array<string> => {
@@ -24,9 +29,9 @@ const collectDates = (document: PeppolDocument): Array<string> => {
24
29
  ...getLines(document).flatMap(line => [line.invoicePeriod?.startDate, line.invoicePeriod?.endDate]),
25
30
  ].filter((date): date is string => date !== undefined);
26
31
 
27
- const dates = [document.issueDate, ...optionalDates].map(date => encodeDateSync(date));
32
+ const dates = [document.issueDate, ...optionalDates].map(date => formatDate(date));
28
33
  if (Predicate.hasProperty(document, 'dueDate') && isDate(document.dueDate)) {
29
- dates.push(encodeDateSync(document.dueDate));
34
+ dates.push(formatDate(document.dueDate));
30
35
  }
31
36
  return dates;
32
37
  };