File tree Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Expand file tree Collapse file tree 3 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ import { isArrayEqual } from '../utils/arrUtils';
27
27
import { Node , Pair } from 'yaml' ;
28
28
import { safeCreateUnicodeRegExp } from '../utils/strings' ;
29
29
import { FilePatternAssociation } from '../services/yamlSchemaService' ;
30
+ import { floatSafeRemainder } from '../utils/math' ;
30
31
31
32
const localize = nls . loadMessageBundle ( ) ;
32
33
const MSG_PROPERTY_NOT_ALLOWED = 'Property {0} is not allowed.' ;
@@ -935,7 +936,7 @@ function validate(
935
936
const val = node . value ;
936
937
937
938
if ( isNumber ( schema . multipleOf ) ) {
938
- if ( val % schema . multipleOf !== 0 ) {
939
+ if ( floatSafeRemainder ( val , schema . multipleOf ) !== 0 ) {
939
940
validationResult . problems . push ( {
940
941
location : { offset : node . offset , length : node . length } ,
941
942
severity : DiagnosticSeverity . Warning ,
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -1541,6 +1541,28 @@ describe('JSON Parser', () => {
1541
1541
}
1542
1542
} ) ;
1543
1543
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
+
1544
1566
it ( 'dependencies with array' , function ( ) {
1545
1567
const schema : JsonSchema . JSONSchema = {
1546
1568
type : 'object' ,
You can’t perform that action at this time.
0 commit comments