tsondb 0.5.2 → 0.5.4

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/lib/node/index.js CHANGED
@@ -5,7 +5,7 @@ import { parallelizeErrors } from "../shared/utils/validation.js";
5
5
  import { getEntities } from "./Schema.js";
6
6
  import { createValidators, validateEntityDecl } from "./schema/index.js";
7
7
  import { createServer } from "./server/index.js";
8
- import { getErrorMessageForDisplay, wrapErrorsIfAny } from "./utils/error.js";
8
+ import { countErrors, getErrorMessageForDisplay, wrapErrorsIfAny } from "./utils/error.js";
9
9
  import { formatInstance, getInstancesByEntityName } from "./utils/instances.js";
10
10
  const debug = Debug("tsondb:schema");
11
11
  const prepareFolders = async (dataRootPath, entities) => {
@@ -21,7 +21,9 @@ export const generateOutputs = async (schema, outputs) => {
21
21
  }
22
22
  };
23
23
  const _validate = (dataRootPath, entities, instancesByEntityName) => {
24
- const errors = entities.flatMap(entity => parallelizeErrors(instancesByEntityName[entity.name]?.map(instance => wrapErrorsIfAny(`in file "${join(dataRootPath, entity.name, instance.fileName)}"`, validateEntityDecl(createValidators(instancesByEntityName), entity, instance.content))) ?? []));
24
+ const errors = entities
25
+ .flatMap(entity => parallelizeErrors(instancesByEntityName[entity.name]?.map(instance => wrapErrorsIfAny(`in file "${join(dataRootPath, entity.name, instance.fileName)}"`, validateEntityDecl(createValidators(instancesByEntityName), entity, instance.content))) ?? []))
26
+ .toSorted((a, b) => a.message.localeCompare(b.message));
25
27
  if (errors.length === 0) {
26
28
  debug("All entities are valid");
27
29
  }
@@ -30,7 +32,7 @@ const _validate = (dataRootPath, entities, instancesByEntityName) => {
30
32
  for (const error of errors) {
31
33
  debug(getErrorMessageForDisplay(error) + "\n");
32
34
  }
33
- throw new Error("Validation failed");
35
+ throw new Error(`Validation failed with ${countErrors(errors).toString()} errors`);
34
36
  }
35
37
  };
36
38
  export const validate = async (schema, dataRootPath) => {
@@ -1,2 +1,4 @@
1
1
  export declare const getErrorMessageForDisplay: (error: Error) => string;
2
+ export declare const countError: (error: Error) => number;
3
+ export declare const countErrors: (errors: Error[]) => number;
2
4
  export declare const wrapErrorsIfAny: (message: string, errors: Error[]) => AggregateError | undefined;
@@ -13,6 +13,12 @@ export const getErrorMessageForDisplay = (error) => {
13
13
  return error.message;
14
14
  }
15
15
  };
16
+ export const countError = (error) => error instanceof AggregateError
17
+ ? countErrors(error.errors.filter(subError => subError instanceof Error))
18
+ : error.cause instanceof Error
19
+ ? countError(error.cause)
20
+ : 1;
21
+ export const countErrors = (errors) => errors.reduce((count, error) => count + countError(error), 0);
16
22
  export const wrapErrorsIfAny = (message, errors) => {
17
23
  if (errors.length === 0) {
18
24
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsondb",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "Lukas Obermann",