Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@vscode/l10n": "^0.0.18",
"ajv": "^8.17.1",
"ajv-draft-04": "^1.0.0",
"ajv-i18n": "^4.2.0",
"request-light": "^0.5.7",
"vscode-json-languageservice": "4.1.8",
"vscode-languageserver": "^9.0.0",
Expand Down
28 changes: 27 additions & 1 deletion src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import { SchemaVersions } from '../yamlTypes';
import { getSchemaFromModeline } from './modelineUtil';

import Ajv, { DefinedError, type AnySchemaObject, type ValidateFunction } from 'ajv';
import Ajv, { DefinedError, type AnySchemaObject, type ErrorObject, type ValidateFunction } from 'ajv';
import Ajv4 from 'ajv-draft-04';
import Ajv2019 from 'ajv/dist/2019';
import Ajv2020 from 'ajv/dist/2020';
import type { Localize } from 'ajv-i18n/localize/types';
import * as Json from 'jsonc-parser';
import { parse } from 'yaml';
import { CRD_CATALOG_URL, KUBERNETES_SCHEMA_URL } from '../utils/schemaUrls';
Expand All @@ -47,6 +48,8 @@
const jsonSchema2019 = require('ajv/dist/refs/json-schema-2019-09/schema.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const jsonSchema2020 = require('ajv/dist/refs/json-schema-2020-12/schema.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ajvLocalizers: Record<string, Localize> = require('ajv-i18n');
Comment thread
datho7561 marked this conversation as resolved.
Dismissed
Comment thread
datho7561 marked this conversation as resolved.

const schema04Validator = ajv4.compile(jsonSchema04);
const schema07Validator = ajv7.compile(jsonSchema07);
Expand All @@ -56,6 +59,11 @@
const schemaDialectCache = new Map<string, SchemaDialect>();
const schemaDialectInFlight = new Map<string, Promise<SchemaDialect>>();

const AJV_LOCALE_ALIASES = new Map<string, string>([
['zh-cn', 'zh'],
['zh-tw', 'zh-TW']

Check failure on line 64 in src/languageservice/services/yamlSchemaService.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Insert `,`

Check failure on line 64 in src/languageservice/services/yamlSchemaService.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Insert `,`
]);

// metadata/keywords that don't add constraints and thus don't count as $ref siblings
const REF_SIBLING_NONCONSTRAINT_KEYS = new Set([
'$ref',
Expand Down Expand Up @@ -264,6 +272,7 @@
* ----------------------------
*/
const _loadSchema = this.loadSchema.bind(this);
const ajvErrorLocale = this.yamlSettings?.locale;
async function _metaValidateSchemaNode(node: JSONSchema, hasNestedSchema: boolean): Promise<void> {
if (!node || typeof node !== 'object') return;
const dialect = await pickSchemaDialect(node.$schema, _loadSchema);
Expand All @@ -283,6 +292,7 @@
}

if (!validator(toValidate)) {
localizeAjvErrors(validator.errors, ajvErrorLocale);
const errs: string[] = [];
for (const err of validator.errors as DefinedError[]) {
errs.push(`${err.instancePath} : ${err.message}`);
Expand Down Expand Up @@ -1433,6 +1443,22 @@
return undefined;
}

function getAjvLocalizer(locale?: string): Localize | undefined {
if (!locale) return undefined;

const lowerLocale = locale.trim().toLowerCase();
const aliasedLocale = AJV_LOCALE_ALIASES.get(lowerLocale);

return ajvLocalizers[locale]

Check failure on line 1452 in src/languageservice/services/yamlSchemaService.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Replace `⏎····||·ajvLocalizers[lowerLocale]⏎···` with `·||·ajvLocalizers[lowerLocale]`

Check failure on line 1452 in src/languageservice/services/yamlSchemaService.ts

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Replace `⏎····||·ajvLocalizers[lowerLocale]⏎···` with `·||·ajvLocalizers[lowerLocale]`
|| ajvLocalizers[lowerLocale]
|| (aliasedLocale ? ajvLocalizers[aliasedLocale] : undefined);
}

function localizeAjvErrors(errors: ErrorObject[] | null | undefined, locale?: string): void {
const localizer = getAjvLocalizer(locale) || ajvLocalizers.en;
localizer(errors);
}

async function pickSchemaDialect(
$schema: string | undefined,
loadSchema?: (uri: string) => Promise<UnresolvedSchema>
Expand Down
1 change: 1 addition & 0 deletions src/yamlServerInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class YAMLServerInit {

// public for test setup
async connectionInitialized(params: InitializeParams): Promise<InitializeResult> {
this.yamlSettings.locale = params.locale || 'en';
this.yamlSettings.capabilities = params.capabilities;
this.languageService = getCustomLanguageService({
schemaRequestService: this.schemaRequestService,
Expand Down
1 change: 1 addition & 0 deletions src/yamlSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class SettingsState {
useSchemaSelectionRequests = false;
hasWsChangeWatchedFileDynamicRegistration = false;
fileExtensions: string[] = ['.yml', '.yaml'];
locale = 'en';
}

export class TextDocumentTestManager extends TextDocuments<TextDocument> {
Expand Down
Loading