5etools-utils 0.5.14 → 0.5.15

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.
Files changed (2) hide show
  1. package/lib/TestJson.js +19 -9
  2. package/package.json +3 -2
package/lib/TestJson.js CHANGED
@@ -33,10 +33,10 @@ class JsonTester {
33
33
  if (isBrew && dirSchema) throw new Error(`"isBrew" and "dirSchema" are mutually exclusive!`);
34
34
  if (!fnGetSchemaId) throw new Error(`"fnGetSchemaId" is required!`);
35
35
 
36
- this._dirSchema = dirSchema ??
37
- (isBrew
38
- ? path.join(__dirname, "..", "schema", "brew")
39
- : path.join(__dirname, "..", "schema", "site"));
36
+ this._dirSchemaSite = path.join(__dirname, "..", "schema", "site");
37
+ this._dirSchemaBrew = path.join(__dirname, "..", "schema", "brew");
38
+
39
+ this._dirSchema = dirSchema ?? (isBrew ? this._dirSchemaBrew : this._dirSchemaSite);
40
40
  this._tagLog = tagLog;
41
41
  this._fnGetSchemaId = fnGetSchemaId;
42
42
 
@@ -112,30 +112,40 @@ class JsonTester {
112
112
  return out;
113
113
  }
114
114
 
115
- _doLoadSchema () {
115
+ doLoadSchema (type, relativeFilePath) {
116
+ if (!["site", "brew"].includes(type)) throw new Error(`Unknown schema type "${type}"`);
117
+
118
+ const dir = type === "site" ? this._dirSchemaSite : this._dirSchemaBrew;
119
+
120
+ this._ajv.addSchema(
121
+ Uf.readJSON(path.join(dir, relativeFilePath)),
122
+ relativeFilePath,
123
+ );
124
+ }
125
+
126
+ _doLoadSchemas () {
116
127
  if (this._isSchemaLoaded) return;
117
128
 
118
129
  Uf.listJsonFiles(this._dirSchema)
119
130
  .forEach(filePath => {
120
131
  filePath = path.normalize(filePath);
121
- const contents = Uf.readJSON(filePath);
122
132
 
123
133
  const relativeFilePath = path.relative(this._dirSchema, filePath)
124
134
  .replace(/\\/g, "/");
125
135
 
126
- this._ajv.addSchema(contents, relativeFilePath);
136
+ this._ajv.addSchema(Uf.readJSON(filePath), relativeFilePath);
127
137
  });
128
138
 
129
139
  this._isSchemaLoaded = true;
130
140
  }
131
141
 
132
142
  _hasSchema (schemaId) {
133
- this._doLoadSchema();
143
+ this._doLoadSchemas();
134
144
  return !!this._ajv.schemas[schemaId];
135
145
  }
136
146
 
137
147
  getFileErrors ({filePath} = {}) {
138
- this._doLoadSchema();
148
+ this._doLoadSchemas();
139
149
 
140
150
  Um.info(this._tagLog, `\tValidating "${filePath}"...`);
141
151
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "5etools-utils",
3
- "version": "0.5.14",
3
+ "version": "0.5.15",
4
4
  "description": "Shared utilities for the 5etools ecosystem.",
5
5
  "type": "module",
6
6
  "main": "lib/Api.js",
@@ -15,7 +15,8 @@
15
15
  "build": "node node/compile-schemas.js",
16
16
  "test": "npm run lint:js && npm run test:js",
17
17
  "test:js": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
18
- "lint:js": "eslint lib node test --fix"
18
+ "lint:js": "eslint lib node test --fix",
19
+ "preversion": "npm t"
19
20
  },
20
21
  "repository": {
21
22
  "type": "git",