From fa8fc9f0cd68617615c5ae742f8a6c84945862ba Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Mon, 12 Aug 2024 01:27:21 -0400 Subject: [PATCH 1/6] Update the .loc.json schema to allow for string names taht include the '$' character. --- ...llow-569XZilmsin-string-names_2024-08-12-05-26.json | 10 ++++++++++ .../src/schemas/locJson.schema.json | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 common/changes/@rushstack/localization-utilities/allow-569XZilmsin-string-names_2024-08-12-05-26.json diff --git a/common/changes/@rushstack/localization-utilities/allow-569XZilmsin-string-names_2024-08-12-05-26.json b/common/changes/@rushstack/localization-utilities/allow-569XZilmsin-string-names_2024-08-12-05-26.json new file mode 100644 index 00000000000..b31bfe04576 --- /dev/null +++ b/common/changes/@rushstack/localization-utilities/allow-569XZilmsin-string-names_2024-08-12-05-26.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/localization-utilities", + "comment": "Update the schema for `.loc.json` files to allow string names that include the `$` character.", + "type": "minor" + } + ], + "packageName": "@rushstack/localization-utilities" +} \ No newline at end of file diff --git a/libraries/localization-utilities/src/schemas/locJson.schema.json b/libraries/localization-utilities/src/schemas/locJson.schema.json index a407d34bf33..68b8f57f71b 100644 --- a/libraries/localization-utilities/src/schemas/locJson.schema.json +++ b/libraries/localization-utilities/src/schemas/locJson.schema.json @@ -9,7 +9,7 @@ } }, "patternProperties": { - "^[A-Za-z_][0-9A-Za-z_]*$": { + "^[A-Za-z_$][0-9A-Za-z_$]*$": { "type": "object", "properties": { "value": { From 73a4f6a3570630073a7ba6b7a9c839a2e5bd3cf6 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Mon, 12 Aug 2024 01:51:23 -0400 Subject: [PATCH 2/6] fixup! Update the .loc.json schema to allow for string names taht include the '$' character. --- .../localization-utilities/src/schemas/locJson.schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/localization-utilities/src/schemas/locJson.schema.json b/libraries/localization-utilities/src/schemas/locJson.schema.json index 68b8f57f71b..8680331d282 100644 --- a/libraries/localization-utilities/src/schemas/locJson.schema.json +++ b/libraries/localization-utilities/src/schemas/locJson.schema.json @@ -9,7 +9,7 @@ } }, "patternProperties": { - "^[A-Za-z_$][0-9A-Za-z_$]*$": { + "^[A-Za-z_$][0-9A-Za-z_$]*(? Date: Mon, 12 Aug 2024 17:24:42 -0400 Subject: [PATCH 3/6] Add a ignoreSchemaField property. --- ...Zilmsin-string-names_2024-08-12-21-24.json | 10 ++++ common/reviews/api/node-core-library.api.md | 9 +++- .../src/parsers/parseLocJson.ts | 2 +- .../src/schemas/locJson.schema.json | 8 +--- libraries/node-core-library/src/JsonSchema.ts | 48 ++++++++++++++----- libraries/node-core-library/src/index.ts | 1 + 6 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 common/changes/@rushstack/node-core-library/allow-569XZilmsin-string-names_2024-08-12-21-24.json diff --git a/common/changes/@rushstack/node-core-library/allow-569XZilmsin-string-names_2024-08-12-21-24.json b/common/changes/@rushstack/node-core-library/allow-569XZilmsin-string-names_2024-08-12-21-24.json new file mode 100644 index 00000000000..aa653a68842 --- /dev/null +++ b/common/changes/@rushstack/node-core-library/allow-569XZilmsin-string-names_2024-08-12-21-24.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@rushstack/node-core-library", + "comment": "Add a `ignoreSchemaField` option to the `JsonSchema.validateObject` options to ignore `$schema` properties and add an options object argument to `JsonSchema.validateObjectWithCallback` with the same `ignoreSchemaField` option.", + "type": "minor" + } + ], + "packageName": "@rushstack/node-core-library" +} \ No newline at end of file diff --git a/common/reviews/api/node-core-library.api.md b/common/reviews/api/node-core-library.api.md index 3ad36597629..359426fe265 100644 --- a/common/reviews/api/node-core-library.api.md +++ b/common/reviews/api/node-core-library.api.md @@ -441,7 +441,12 @@ export interface IJsonSchemaLoadOptions { } // @public -export interface IJsonSchemaValidateOptions { +export interface IJsonSchemaValidateObjectWithOptions { + ignoreSchemaField?: boolean; +} + +// @public +export interface IJsonSchemaValidateOptions extends IJsonSchemaValidateObjectWithOptions { customErrorHeader?: string; } @@ -675,7 +680,7 @@ export class JsonSchema { static fromLoadedObject(schemaObject: JsonObject, options?: IJsonSchemaFromObjectOptions): JsonSchema; get shortName(): string; validateObject(jsonObject: JsonObject, filenameForErrors: string, options?: IJsonSchemaValidateOptions): void; - validateObjectWithCallback(jsonObject: JsonObject, errorCallback: (errorInfo: IJsonSchemaErrorInfo) => void): void; + validateObjectWithCallback(jsonObject: JsonObject, errorCallback: (errorInfo: IJsonSchemaErrorInfo) => void, options?: IJsonSchemaValidateObjectWithOptions): void; } // @public diff --git a/libraries/localization-utilities/src/parsers/parseLocJson.ts b/libraries/localization-utilities/src/parsers/parseLocJson.ts index a49d2efdd18..ab267ae5d99 100644 --- a/libraries/localization-utilities/src/parsers/parseLocJson.ts +++ b/libraries/localization-utilities/src/parsers/parseLocJson.ts @@ -14,7 +14,7 @@ const LOC_JSON_SCHEMA: JsonSchema = JsonSchema.fromLoadedObject(locJsonSchema); export function parseLocJson({ content, filePath, ignoreString }: IParseFileOptions): ILocalizationFile { const parsedFile: ILocalizationFile = JsonFile.parseString(content); try { - LOC_JSON_SCHEMA.validateObject(parsedFile, filePath); + LOC_JSON_SCHEMA.validateObject(parsedFile, filePath, { ignoreSchemaField: true }); } catch (e) { throw new Error(`The loc file is invalid. Error: ${e}`); } diff --git a/libraries/localization-utilities/src/schemas/locJson.schema.json b/libraries/localization-utilities/src/schemas/locJson.schema.json index 8680331d282..60cd6027798 100644 --- a/libraries/localization-utilities/src/schemas/locJson.schema.json +++ b/libraries/localization-utilities/src/schemas/locJson.schema.json @@ -2,14 +2,8 @@ "$schema": "http://json-schema.org/draft-04/schema#", "title": "Localizable JSON file", - "properties": { - "$schema": { - "description": "Part of the JSON Schema standard, this optional keyword declares the URL of the schema that the file conforms to. Editors may download the schema and use it to perform syntax highlighting.", - "type": "string" - } - }, "patternProperties": { - "^[A-Za-z_$][0-9A-Za-z_$]*(? { - const prefix: string = - options && options.customErrorHeader ? options.customErrorHeader : 'JSON validation failed:'; - - throw new Error(prefix + os.EOL + filenameForErrors + os.EOL + errorInfo.details); - }); + this.validateObjectWithCallback( + jsonObject, + (errorInfo: IJsonSchemaErrorInfo) => { + const prefix: string = + options && options.customErrorHeader ? options.customErrorHeader : 'JSON validation failed:'; + + throw new Error(prefix + os.EOL + filenameForErrors + os.EOL + errorInfo.details); + }, + options + ); } /** @@ -348,10 +364,16 @@ export class JsonSchema { */ public validateObjectWithCallback( jsonObject: JsonObject, - errorCallback: (errorInfo: IJsonSchemaErrorInfo) => void + errorCallback: (errorInfo: IJsonSchemaErrorInfo) => void, + options?: IJsonSchemaValidateObjectWithOptions ): void { this.ensureCompiled(); + if (options?.ignoreSchemaField) { + jsonObject = { ...jsonObject }; + delete jsonObject.$schema; + } + if (this._validator && !this._validator(jsonObject)) { const errorDetails: string = JsonSchema._formatErrorDetails(this._validator.errors!); diff --git a/libraries/node-core-library/src/index.ts b/libraries/node-core-library/src/index.ts index a8d46cdaedd..70396912fbc 100644 --- a/libraries/node-core-library/src/index.ts +++ b/libraries/node-core-library/src/index.ts @@ -69,6 +69,7 @@ export { type IJsonSchemaFromObjectOptions, type IJsonSchemaLoadOptions, type IJsonSchemaValidateOptions, + type IJsonSchemaValidateObjectWithOptions, JsonSchema, type JsonSchemaVersion } from './JsonSchema'; From 84d733de91194ff9eb307ca12b7ad71d59b8a1c7 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Mon, 12 Aug 2024 17:38:39 -0400 Subject: [PATCH 4/6] Update libraries/node-core-library/src/JsonSchema.ts Co-authored-by: David Michon --- libraries/node-core-library/src/JsonSchema.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libraries/node-core-library/src/JsonSchema.ts b/libraries/node-core-library/src/JsonSchema.ts index d1afac91179..a9506eeea2c 100644 --- a/libraries/node-core-library/src/JsonSchema.ts +++ b/libraries/node-core-library/src/JsonSchema.ts @@ -370,8 +370,11 @@ export class JsonSchema { this.ensureCompiled(); if (options?.ignoreSchemaField) { - jsonObject = { ...jsonObject }; - delete jsonObject.$schema; + const { + $schema, + ...remainder + } = jsonObject; + jsonObject = remainder; } if (this._validator && !this._validator(jsonObject)) { From 892446029c21c0ba2254f07a10424b22ed32597e Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Mon, 12 Aug 2024 17:39:11 -0400 Subject: [PATCH 5/6] Update libraries/node-core-library/src/JsonSchema.ts --- libraries/node-core-library/src/JsonSchema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/node-core-library/src/JsonSchema.ts b/libraries/node-core-library/src/JsonSchema.ts index a9506eeea2c..033a42cd63c 100644 --- a/libraries/node-core-library/src/JsonSchema.ts +++ b/libraries/node-core-library/src/JsonSchema.ts @@ -350,7 +350,7 @@ export class JsonSchema { jsonObject, (errorInfo: IJsonSchemaErrorInfo) => { const prefix: string = - options && options.customErrorHeader ? options.customErrorHeader : 'JSON validation failed:'; + options?.customErrorHeader ?? 'JSON validation failed:'; throw new Error(prefix + os.EOL + filenameForErrors + os.EOL + errorInfo.details); }, From 88e9da1e10a4f073c505c6ee100d06727c2bc9a8 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Mon, 12 Aug 2024 17:41:25 -0400 Subject: [PATCH 6/6] fixup! Update libraries/node-core-library/src/JsonSchema.ts --- libraries/node-core-library/src/JsonSchema.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/node-core-library/src/JsonSchema.ts b/libraries/node-core-library/src/JsonSchema.ts index 033a42cd63c..fd68ad65302 100644 --- a/libraries/node-core-library/src/JsonSchema.ts +++ b/libraries/node-core-library/src/JsonSchema.ts @@ -349,8 +349,7 @@ export class JsonSchema { this.validateObjectWithCallback( jsonObject, (errorInfo: IJsonSchemaErrorInfo) => { - const prefix: string = - options?.customErrorHeader ?? 'JSON validation failed:'; + const prefix: string = options?.customErrorHeader ?? 'JSON validation failed:'; throw new Error(prefix + os.EOL + filenameForErrors + os.EOL + errorInfo.details); }, @@ -371,6 +370,7 @@ export class JsonSchema { if (options?.ignoreSchemaField) { const { + // eslint-disable-next-line @typescript-eslint/no-unused-vars $schema, ...remainder } = jsonObject;