@salesforce/core 2.36.3 → 2.36.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/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [2.36.4](https://github.com/forcedotcom/sfdx-core/compare/v2.36.3...v2.36.4) (2022-04-28)
6
+
7
+ ### Bug Fixes
8
+
9
+ - disable strictSchema ([2f44345](https://github.com/forcedotcom/sfdx-core/commit/2f44345d6dc11673b921b6c65cd548b7f31a0dad))
10
+ - replace JSEN with AJV (wip) ([87ca60c](https://github.com/forcedotcom/sfdx-core/commit/87ca60c48a82618008d761bdd6086727f712aeff))
11
+ - schema comment ([a0868d1](https://github.com/forcedotcom/sfdx-core/commit/a0868d157fbc8757cdbd28472aecfbf597c0f9d2))
12
+ - test failures and cleanup ([95c0710](https://github.com/forcedotcom/sfdx-core/commit/95c0710938b399c858cddfdf5100892ee2e7cf90))
13
+
5
14
  ### [2.36.3](https://github.com/forcedotcom/sfdx-core/compare/v2.36.2...v2.36.3) (2022-04-21)
6
15
 
7
16
  ### Bug Fixes
@@ -46,10 +46,12 @@ export declare class SchemaValidator {
46
46
  */
47
47
  validateSync(json: AnyJson): AnyJson;
48
48
  /**
49
- * Loads local, external schemas from URIs relative to the local schema file. Does not support loading from
50
- * remote URIs. Returns a map of external schema local URIs to loaded schema JSON objects.
49
+ * Loads local, external schemas from URIs in the same directory as the local schema file.
50
+ * Does not support loading from remote URIs.
51
+ * Returns a map of external schema local URIs to loaded schema JSON objects.
51
52
  *
52
- * @param schema The main schema to validate against.
53
+ * @param schema The main schema to look up references ($ref) in.
54
+ * @returns An array of found referenced schemas.
53
55
  */
54
56
  private loadExternalSchemas;
55
57
  /**
@@ -60,9 +62,9 @@ export declare class SchemaValidator {
60
62
  private loadExternalSchema;
61
63
  /**
62
64
  * Get a string representation of the schema validation errors.
65
+ * Adds additional (human friendly) information to certain errors.
63
66
  *
64
- * @param errors An array of JsenValidateError objects.
65
- * @param schema The validation schema.
67
+ * @param errors An array of AJV (DefinedError) objects.
66
68
  */
67
69
  private getErrorsText;
68
70
  }
@@ -7,12 +7,11 @@
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.SchemaValidator = void 0;
10
+ const fs = require("fs");
10
11
  const path = require("path");
12
+ const ajv_1 = require("ajv");
11
13
  const kit_1 = require("@salesforce/kit");
12
- const ts_types_1 = require("@salesforce/ts-types");
13
- const validator = require("jsen");
14
14
  const sfdxError_1 = require("../sfdxError");
15
- const fs_1 = require("../util/fs");
16
15
  /**
17
16
  * Loads a JSON schema and performs validations against JSON objects.
18
17
  */
@@ -33,7 +32,7 @@ class SchemaValidator {
33
32
  */
34
33
  async load() {
35
34
  if (!this.schema) {
36
- this.schema = await fs_1.fs.readJsonMap(this.schemaPath);
35
+ this.schema = kit_1.parseJsonMap(await fs.promises.readFile(this.schemaPath, 'utf8'));
37
36
  this.logger.debug(`Schema loaded for ${this.schemaPath}`);
38
37
  }
39
38
  return this.schema;
@@ -43,7 +42,7 @@ class SchemaValidator {
43
42
  */
44
43
  loadSync() {
45
44
  if (!this.schema) {
46
- this.schema = fs_1.fs.readJsonMapSync(this.schemaPath);
45
+ this.schema = kit_1.parseJsonMap(fs.readFileSync(this.schemaPath, 'utf8'));
47
46
  this.logger.debug(`Schema loaded for ${this.schemaPath}`);
48
47
  }
49
48
  return this.schema;
@@ -75,47 +74,54 @@ class SchemaValidator {
75
74
  validateSync(json) {
76
75
  const schema = this.loadSync();
77
76
  const externalSchemas = this.loadExternalSchemas(schema);
77
+ const ajv = new ajv_1.default({
78
+ allErrors: true,
79
+ schemas: externalSchemas,
80
+ useDefaults: true,
81
+ // TODO: We may someday want to enable strictSchema. This is disabled for now
82
+ // because the CLI team does not "own" the @salesforce/schemas repository.
83
+ // Invalid schema would cause errors wherever SchemaValidator is used.
84
+ strictSchema: false,
85
+ });
86
+ // JSEN to AJV migration note - regarding the following "TODO":
87
+ // I don't think that AJV has a way to throw an error if an additional property exists in the data
88
+ // It does however have a top level option for `removeAdditional` https://ajv.js.org/options.html#removeadditional
89
+ // Regardless, this would be a breaking changes and I do not think it should be implemented.
78
90
  // TODO: We should default to throw an error when a property is specified
79
91
  // that is not in the schema, but the only option to do this right now is
80
92
  // to specify "removeAdditional: false" in every object.
81
- const validate = validator(schema, {
82
- greedy: true,
83
- schemas: externalSchemas,
84
- });
85
- if (!validate(json)) {
93
+ const validate = ajv.compile(schema);
94
+ // AJV will modify the original json object. We need to make a clone of the
95
+ // json to keep this backwards compatible with JSEN functionality
96
+ const jsonClone = JSON.parse(JSON.stringify(json));
97
+ const valid = validate(jsonClone);
98
+ if (!valid) {
86
99
  if (validate.errors) {
87
- const errors = this.getErrorsText(validate.errors, schema);
100
+ const errors = this.getErrorsText(validate.errors);
88
101
  throw new sfdxError_1.SfdxError(`Validation errors:\n${errors}`, 'ValidationSchemaFieldErrors');
89
102
  }
90
103
  else {
91
104
  throw new sfdxError_1.SfdxError('Unknown schema validation error', 'ValidationSchemaUnknown');
92
105
  }
93
106
  }
94
- return validate.build(json);
107
+ // We return the cloned JSON because it will have defaults included
108
+ // This is configured with the 'useDefaults' option above.
109
+ return jsonClone;
95
110
  }
96
111
  /**
97
- * Loads local, external schemas from URIs relative to the local schema file. Does not support loading from
98
- * remote URIs. Returns a map of external schema local URIs to loaded schema JSON objects.
112
+ * Loads local, external schemas from URIs in the same directory as the local schema file.
113
+ * Does not support loading from remote URIs.
114
+ * Returns a map of external schema local URIs to loaded schema JSON objects.
99
115
  *
100
- * @param schema The main schema to validate against.
116
+ * @param schema The main schema to look up references ($ref) in.
117
+ * @returns An array of found referenced schemas.
101
118
  */
102
119
  loadExternalSchemas(schema) {
103
- const externalSchemas = {};
104
- const schemas = kit_1.getJsonValuesByName(schema, '$ref')
105
- // eslint-disable-next-line no-useless-escape
106
- .map((ref) => ref && RegExp(/([\w\.]+)#/).exec(ref))
120
+ return kit_1.getJsonValuesByName(schema, '$ref')
121
+ .map((ref) => ref && RegExp(/([\w\.]+)#/).exec(ref)) // eslint-disable-line no-useless-escape
107
122
  .map((match) => match && match[1])
108
123
  .filter((uri) => !!uri)
109
124
  .map((uri) => this.loadExternalSchema(uri));
110
- schemas.forEach((externalSchema) => {
111
- if (ts_types_1.isString(externalSchema.id)) {
112
- externalSchemas[externalSchema.id] = externalSchema;
113
- }
114
- else {
115
- throw new sfdxError_1.SfdxError(`Unexpected external schema id type: ${typeof externalSchema.id}`, 'ValidationSchemaTypeError');
116
- }
117
- });
118
- return externalSchemas;
119
125
  }
120
126
  /**
121
127
  * Load another schema relative to the primary schema when referenced. Only supports local schema URIs.
@@ -125,7 +131,7 @@ class SchemaValidator {
125
131
  loadExternalSchema(uri) {
126
132
  const schemaPath = path.join(this.schemasDir, `${uri}.json`);
127
133
  try {
128
- return fs_1.fs.readJsonMapSync(schemaPath);
134
+ return kit_1.parseJsonMap(fs.readFileSync(schemaPath, 'utf8'));
129
135
  }
130
136
  catch (err) {
131
137
  if (err.code === 'ENOENT') {
@@ -136,52 +142,21 @@ class SchemaValidator {
136
142
  }
137
143
  /**
138
144
  * Get a string representation of the schema validation errors.
145
+ * Adds additional (human friendly) information to certain errors.
139
146
  *
140
- * @param errors An array of JsenValidateError objects.
141
- * @param schema The validation schema.
147
+ * @param errors An array of AJV (DefinedError) objects.
142
148
  */
143
- getErrorsText(errors, schema) {
149
+ getErrorsText(errors) {
144
150
  return errors
145
151
  .map((error) => {
146
- // eslint-disable-next-line no-useless-escape
147
- const property = RegExp(/^([a-zA-Z0-9\.]+)\.([a-zA-Z0-9]+)$/).exec(error.path);
148
- const getPropValue = (prop) => {
149
- const reducer = (obj, name) => {
150
- if (!ts_types_1.isJsonMap(obj))
151
- return;
152
- if (ts_types_1.isJsonMap(obj.properties))
153
- return obj.properties[name];
154
- if (name === '0')
155
- return ts_types_1.asJsonArray(obj.items);
156
- return obj[name] || obj[prop];
157
- };
158
- return error.path.split('.').reduce(reducer, schema);
159
- };
160
- const getEnumValues = () => {
161
- const enumSchema = ts_types_1.asJsonMap(getPropValue('enum'));
162
- return (enumSchema && ts_types_1.getJsonArray(enumSchema, 'enum', []).join(', ')) || '';
163
- };
152
+ const msg = `${error.schemaPath}: ${error.message}`;
164
153
  switch (error.keyword) {
165
154
  case 'additionalProperties':
166
- // Missing Typing
167
- // eslint-disable-next-line no-case-declarations
168
- const additionalProperties = ts_types_1.get(error, 'additionalProperties');
169
- return `${error.path} should NOT have additional properties '${additionalProperties}'`;
170
- case 'required':
171
- if (property) {
172
- return `${property[1]} should have required property ${property[2]}`;
173
- }
174
- return `should have required property '${error.path}'`;
175
- case 'oneOf':
176
- return `${error.path} should match exactly one schema in oneOf`;
155
+ return `${msg} '${error.params.additionalProperty}'`;
177
156
  case 'enum':
178
- return `${error.path} should be equal to one of the allowed values ${getEnumValues()}`;
179
- case 'type': {
180
- const _path = error.path === '' ? 'Root of JSON object' : error.path;
181
- return `${_path} is an invalid type. Expected type [${getPropValue('type')}]`;
182
- }
157
+ return `${msg} '${error.params.allowedValues.join(', ')}'`;
183
158
  default:
184
- return `${error.path} invalid ${error.keyword}`;
159
+ return msg;
185
160
  }
186
161
  })
187
162
  .join('\n');
@@ -81,7 +81,7 @@ export declare class SfdxProjectJson extends ConfigFile<ConfigFile.Options> {
81
81
  * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
82
82
  * A warning is logged by default when the file is invalid.
83
83
  *
84
- * ***See*** [sfdx-project.schema.json] (https://raw.githubusercontent.com/forcedotcom/schemas/master/schemas/sfdx-project.schema.json)
84
+ * ***See*** [sfdx-project.schema.json] (https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
85
85
  */
86
86
  schemaValidate(): Promise<void>;
87
87
  /**
@@ -95,7 +95,7 @@ export declare class SfdxProjectJson extends ConfigFile<ConfigFile.Options> {
95
95
  * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
96
96
  * A warning is logged by default when the file is invalid.
97
97
  *
98
- * ***See*** [sfdx-project.schema.json] (https://raw.githubusercontent.com/forcedotcom/schemas/master/schemas/sfdx-project.schema.json)
98
+ * ***See*** [sfdx-project.schema.json] (https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
99
99
  */
100
100
  schemaValidateSync(): void;
101
101
  /**
@@ -87,7 +87,7 @@ class SfdxProjectJson extends configFile_1.ConfigFile {
87
87
  * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
88
88
  * A warning is logged by default when the file is invalid.
89
89
  *
90
- * ***See*** [sfdx-project.schema.json] (https://raw.githubusercontent.com/forcedotcom/schemas/master/schemas/sfdx-project.schema.json)
90
+ * ***See*** [sfdx-project.schema.json] (https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
91
91
  */
92
92
  async schemaValidate() {
93
93
  if (!this.hasRead) {
@@ -98,7 +98,6 @@ class SfdxProjectJson extends configFile_1.ConfigFile {
98
98
  try {
99
99
  const projectJsonSchemaPath = require.resolve('@salesforce/schemas/sfdx-project.schema.json');
100
100
  const validator = new validator_1.SchemaValidator(this.logger, projectJsonSchemaPath);
101
- await validator.load();
102
101
  await validator.validate(this.getContents());
103
102
  }
104
103
  catch (err) {
@@ -129,7 +128,7 @@ class SfdxProjectJson extends configFile_1.ConfigFile {
129
128
  * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
130
129
  * A warning is logged by default when the file is invalid.
131
130
  *
132
- * ***See*** [sfdx-project.schema.json] (https://raw.githubusercontent.com/forcedotcom/schemas/master/schemas/sfdx-project.schema.json)
131
+ * ***See*** [sfdx-project.schema.json] (https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
133
132
  */
134
133
  schemaValidateSync() {
135
134
  if (!this.hasRead) {
@@ -140,7 +139,6 @@ class SfdxProjectJson extends configFile_1.ConfigFile {
140
139
  try {
141
140
  const projectJsonSchemaPath = require.resolve('@salesforce/schemas/sfdx-project.schema.json');
142
141
  const validator = new validator_1.SchemaValidator(this.logger, projectJsonSchemaPath);
143
- validator.loadSync();
144
142
  validator.validateSync(this.getContents());
145
143
  }
146
144
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/core",
3
- "version": "2.36.3",
3
+ "version": "2.36.4",
4
4
  "description": "Core libraries to interact with SFDX projects, orgs, and APIs.",
5
5
  "main": "lib/exported",
6
6
  "types": "lib/exported.d.ts",
@@ -41,12 +41,12 @@
41
41
  "@types/graceful-fs": "^4.1.5",
42
42
  "@types/jsforce": "^1.9.41",
43
43
  "@types/mkdirp": "^1.0.1",
44
+ "ajv": "^8.11.0",
44
45
  "archiver": "^5.3.0",
45
46
  "debug": "^3.1.0",
46
47
  "faye": "^1.4.0",
47
48
  "graceful-fs": "^4.2.4",
48
49
  "js2xmlparser": "^4.0.1",
49
- "jsen": "0.6.6",
50
50
  "jsforce": "^1.11.0",
51
51
  "jsonwebtoken": "8.5.0",
52
52
  "mkdirp": "1.0.4",
@@ -60,7 +60,6 @@
60
60
  "@salesforce/ts-sinon": "1.3.21",
61
61
  "@types/archiver": "^5.1.1",
62
62
  "@types/debug": "0.0.30",
63
- "@types/jsen": "0.0.19",
64
63
  "@types/jsonwebtoken": "8.3.2",
65
64
  "@types/semver": "^7.3.9",
66
65
  "@types/shelljs": "0.7.8",