@salesforce/core 3.15.0 → 3.15.1

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,12 @@
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
+ ### [3.15.1](https://github.com/forcedotcom/sfdx-core/compare/v3.15.0...v3.15.1) (2022-04-28)
6
+
7
+ ### Bug Fixes
8
+
9
+ - replace jsen ([664da58](https://github.com/forcedotcom/sfdx-core/commit/664da58cbe06019465ccf70ed17dfafb2425e06b))
10
+
5
11
  ## [3.15.0](https://github.com/forcedotcom/sfdx-core/compare/v3.14.0...v3.15.0) (2022-04-26)
6
12
 
7
13
  ### Features
@@ -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
  }
@@ -9,9 +9,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.SchemaValidator = void 0;
10
10
  const path = require("path");
11
11
  const fs = require("fs");
12
+ const ajv_1 = require("ajv");
12
13
  const kit_1 = require("@salesforce/kit");
13
- const ts_types_1 = require("@salesforce/ts-types");
14
- const validator = require("jsen");
15
14
  const sfError_1 = require("../sfError");
16
15
  /**
17
16
  * Loads a JSON schema and performs validations against JSON objects.
@@ -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 sfError_1.SfError(`Validation errors:\n${errors}`, 'ValidationSchemaFieldError');
89
102
  }
90
103
  else {
91
104
  throw new sfError_1.SfError('Unknown schema validation error', 'ValidationSchemaUnknownError');
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 = (0, kit_1.getJsonValuesByName)(schema, '$ref')
105
- // eslint-disable-next-line no-useless-escape
106
- .map((ref) => ref && RegExp(/([\w\.]+)#/).exec(ref))
120
+ return (0, 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 ((0, ts_types_1.isString)(externalSchema.id)) {
112
- externalSchemas[externalSchema.id] = externalSchema;
113
- }
114
- else {
115
- throw new sfError_1.SfError(`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.
@@ -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 (!(0, ts_types_1.isJsonMap)(obj))
151
- return;
152
- if ((0, ts_types_1.isJsonMap)(obj.properties))
153
- return obj.properties[name];
154
- if (name === '0')
155
- return (0, 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 = (0, ts_types_1.asJsonMap)(getPropValue('enum'));
162
- return (enumSchema && (0, 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 = (0, 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');
@@ -80,7 +80,7 @@ export declare class SfProjectJson extends ConfigFile {
80
80
  * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
81
81
  * A warning is logged by default when the file is invalid.
82
82
  *
83
- * ***See*** [sfdx-project.schema.json] (https://raw.githubusercontent.com/forcedotcom/schemas/master/schemas/sfdx-project.schema.json)
83
+ * ***See*** [sfdx-project.schema.json] ((https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
84
84
  */
85
85
  schemaValidate(): Promise<void>;
86
86
  /**
@@ -94,7 +94,7 @@ export declare class SfProjectJson extends ConfigFile {
94
94
  * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
95
95
  * A warning is logged by default when the file is invalid.
96
96
  *
97
- * ***See*** [sfdx-project.schema.json] (https://raw.githubusercontent.com/forcedotcom/schemas/master/schemas/sfdx-project.schema.json)
97
+ * ***See*** [sfdx-project.schema.json] ((https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
98
98
  */
99
99
  schemaValidateSync(): void;
100
100
  /**
package/lib/sfProject.js CHANGED
@@ -95,7 +95,7 @@ class SfProjectJson extends configFile_1.ConfigFile {
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
  async schemaValidate() {
101
101
  if (!this.hasRead) {
@@ -106,7 +106,6 @@ class SfProjectJson extends configFile_1.ConfigFile {
106
106
  try {
107
107
  const projectJsonSchemaPath = require.resolve('@salesforce/schemas/sfdx-project.schema.json');
108
108
  const validator = new validator_1.SchemaValidator(this.logger, projectJsonSchemaPath);
109
- await validator.load();
110
109
  await validator.validate(this.getContents());
111
110
  }
112
111
  catch (err) {
@@ -135,7 +134,7 @@ class SfProjectJson extends configFile_1.ConfigFile {
135
134
  * Set the `SFDX_PROJECT_JSON_VALIDATION` environment variable to `true` to throw an error when schema validation fails.
136
135
  * A warning is logged by default when the file is invalid.
137
136
  *
138
- * ***See*** [sfdx-project.schema.json] (https://raw.githubusercontent.com/forcedotcom/schemas/master/schemas/sfdx-project.schema.json)
137
+ * ***See*** [sfdx-project.schema.json] ((https://github.com/forcedotcom/schemas/blob/main/sfdx-project.schema.json)
139
138
  */
140
139
  schemaValidateSync() {
141
140
  if (!this.hasRead) {
@@ -146,7 +145,6 @@ class SfProjectJson extends configFile_1.ConfigFile {
146
145
  try {
147
146
  const projectJsonSchemaPath = require.resolve('@salesforce/schemas/sfdx-project.schema.json');
148
147
  const validator = new validator_1.SchemaValidator(this.logger, projectJsonSchemaPath);
149
- validator.loadSync();
150
148
  validator.validateSync(this.getContents());
151
149
  }
152
150
  catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/core",
3
- "version": "3.15.0",
3
+ "version": "3.15.1",
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,6 +41,7 @@
41
41
  "@types/graceful-fs": "^4.1.5",
42
42
  "@types/mkdirp": "^1.0.2",
43
43
  "@types/semver": "^7.3.9",
44
+ "ajv": "^8.11.0",
44
45
  "archiver": "^5.3.0",
45
46
  "change-case": "^4.1.2",
46
47
  "debug": "^3.2.7",
@@ -48,7 +49,6 @@
48
49
  "form-data": "^4.0.0",
49
50
  "graceful-fs": "^4.2.9",
50
51
  "js2xmlparser": "^4.0.1",
51
- "jsen": "0.6.6",
52
52
  "jsforce": "2.0.0-beta.7",
53
53
  "jsonwebtoken": "8.5.1",
54
54
  "mkdirp": "1.0.4",