Skip to content

Commit e259f8f

Browse files
authored
Merge branch 'main' into fix/anyOf-compare-alternatives
2 parents fd8f729 + c1cab4d commit e259f8f

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "yaml-language-server",
33
"description": "YAML language server",
4-
"version": "1.17.0",
4+
"version": "1.18.0",
55
"author": "Red Hat",
66
"license": "MIT",
77
"contributors": [

src/languageservice/parser/jsonParser07.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { isArrayEqual } from '../utils/arrUtils';
2727
import { Node, Pair } from 'yaml';
2828
import { safeCreateUnicodeRegExp } from '../utils/strings';
2929
import { FilePatternAssociation } from '../services/yamlSchemaService';
30+
import { floatSafeRemainder } from '../utils/math';
3031

3132
const localize = nls.loadMessageBundle();
3233
const MSG_PROPERTY_NOT_ALLOWED = 'Property {0} is not allowed.';
@@ -938,7 +939,7 @@ function validate(
938939
const val = node.value;
939940

940941
if (isNumber(schema.multipleOf)) {
941-
if (val % schema.multipleOf !== 0) {
942+
if (floatSafeRemainder(val, schema.multipleOf) !== 0) {
942943
validationResult.problems.push({
943944
location: { offset: node.offset, length: node.length },
944945
severity: DiagnosticSeverity.Warning,

src/languageservice/services/yamlFormatter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
import { Range, Position, TextEdit, FormattingOptions } from 'vscode-languageserver-types';
88
import { CustomFormatterOptions, LanguageSettings } from '../yamlLanguageService';
9-
import { format, Options } from 'prettier';
9+
import { Options } from 'prettier';
1010
import * as parser from 'prettier/plugins/yaml';
11+
import { format } from 'prettier/standalone';
1112
import { TextDocument } from 'vscode-languageserver-textdocument';
1213

1314
export class YAMLFormatter {

src/languageservice/utils/math.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function floatSafeRemainder(val: number, step: number): number {
2+
const valDecCount = (val.toString().split('.')[1] || '').length;
3+
const stepDecCount = (step.toString().split('.')[1] || '').length;
4+
const decCount = Math.max(valDecCount, stepDecCount);
5+
const valInt = parseInt(val.toFixed(decCount).replace('.', ''));
6+
const stepInt = parseInt(step.toFixed(decCount).replace('.', ''));
7+
return (valInt % stepInt) / Math.pow(10, decCount);
8+
}

test/jsonParser.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,28 @@ describe('JSON Parser', () => {
15411541
}
15421542
});
15431543

1544+
it('multipleOfFloat', function () {
1545+
const schema: JsonSchema.JSONSchema = {
1546+
type: 'array',
1547+
items: {
1548+
type: 'number',
1549+
multipleOf: 0.05,
1550+
},
1551+
};
1552+
{
1553+
const { textDoc, jsonDoc } = toDocument('[0.9]');
1554+
const semanticErrors = jsonDoc.validate(textDoc, schema);
1555+
1556+
assert.strictEqual(semanticErrors.length, 0);
1557+
}
1558+
{
1559+
const { textDoc, jsonDoc } = toDocument('[42.3222222]');
1560+
const semanticErrors = jsonDoc.validate(textDoc, schema);
1561+
1562+
assert.strictEqual(semanticErrors.length, 1);
1563+
}
1564+
});
1565+
15441566
it('dependencies with array', function () {
15451567
const schema: JsonSchema.JSONSchema = {
15461568
type: 'object',

0 commit comments

Comments
 (0)