@evergis/common 1.0.3 → 1.0.4-alpha.2

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/dist/index.js CHANGED
@@ -63,6 +63,7 @@ const SPEC_PROP_VALUE_OPTIONS = {
63
63
  transitionable: true,
64
64
  valueTypeAllowed: "number",
65
65
  arrayValue: true,
66
+ arrayOnly: true,
66
67
  },
67
68
  // fill
68
69
  "fill-color": {
@@ -152,6 +153,7 @@ const SPEC_PROP_VALUE_OPTIONS = {
152
153
  transitionable: true,
153
154
  valueTypeAllowed: "number",
154
155
  arrayValue: true,
156
+ arrayOnly: true,
155
157
  },
156
158
  // icon
157
159
  "icon-image": {
@@ -184,6 +186,7 @@ const SPEC_PROP_VALUE_OPTIONS = {
184
186
  transitionable: true,
185
187
  valueTypeAllowed: "number",
186
188
  arrayValue: true,
189
+ arrayOnly: true,
187
190
  },
188
191
  "icon-halo-color": {
189
192
  dataExpressionSupport: true,
@@ -284,6 +287,7 @@ const SPEC_PROP_VALUE_OPTIONS = {
284
287
  transitionable: true,
285
288
  valueTypeAllowed: "number",
286
289
  arrayValue: true,
290
+ arrayOnly: true,
287
291
  },
288
292
  "text-halo-color": {
289
293
  dataExpressionSupport: true,
@@ -349,23 +353,38 @@ const SPEC_PROP_VALUE_OPTIONS = {
349
353
  },
350
354
  };
351
355
 
352
- /* eslint-disable max-lines, no-console, class-methods-use-this, @typescript-eslint/no-magic-numbers */
356
+ /* eslint-disable max-lines, class-methods-use-this, @typescript-eslint/no-magic-numbers */
353
357
  class ClientStyleValidator {
354
- options;
355
- constructor(options) {
356
- this.options = options || {};
358
+ errors;
359
+ constructor() {
360
+ this.errors = [];
357
361
  }
358
362
  validate(clientStyle) {
363
+ this.errors = [];
359
364
  if (lodash.isNil(clientStyle) || lodash.isEmpty(clientStyle) || lodash.isNil(clientStyle?.items) || lodash.isEmpty(clientStyle?.items)) {
360
- return true;
365
+ return { valid: true, errors: [] };
361
366
  }
362
- if (clientStyle.items.some(item => lodash.isNil(item.paint))) {
363
- if (this.options.verbose) {
364
- console.error("Validate client style:", "`paint` prop is mandatory", "\n");
365
- }
366
- return false;
367
+ const noPaintItemIndex = clientStyle.items.findIndex(item => lodash.isNil(item.paint));
368
+ if (noPaintItemIndex > -1) {
369
+ return {
370
+ valid: false,
371
+ errors: [
372
+ {
373
+ path: `item[${noPaintItemIndex}]`,
374
+ message: "The `paint` prop is required",
375
+ },
376
+ ],
377
+ };
367
378
  }
368
- return clientStyle.items.every(item => Object.entries({ ...item.layout, ...item.paint }).every(([key, value]) => this.validateSpecPropValue(key, value)));
379
+ clientStyle.items.forEach((item, itemIndex) => {
380
+ Object.entries({ ...item.layout }).forEach(([key, value]) => {
381
+ this.validateSpecPropValue(`item[${itemIndex}].layout`, key, value);
382
+ });
383
+ Object.entries({ ...item.paint }).forEach(([key, value]) => {
384
+ this.validateSpecPropValue(`item[${itemIndex}].paint`, key, value);
385
+ });
386
+ });
387
+ return { valid: this.errors.length === 0, errors: this.errors };
369
388
  }
370
389
  getSpecPropValueOptions(key) {
371
390
  return SPEC_PROP_VALUE_OPTIONS[key];
@@ -387,393 +406,422 @@ class ClientStyleValidator {
387
406
  .every(e => typeof e === "string"));
388
407
  }
389
408
  isZoomInterpolate(value) {
390
- return Array.isArray(value) && value[0] === "interpolate" && Array.isArray(value[2]) && value[2][0] === "zoom";
409
+ return Array.isArray(value) && value[0] === "interpolate" && lodash.isEqual(value[2], ["zoom"]);
391
410
  }
392
- validateSupportedConcatExpression(key, value) {
411
+ validateSupportedConcatExpression(path, key, value) {
412
+ const errorsCount = this.errors.length;
393
413
  if (key !== "text-field") {
394
- if (this.options.verbose) {
395
- console.error("Validate supported concat expression:", "supported only for `text-field`", key, value, "\n");
396
- }
397
- return false;
414
+ this.errors.push({
415
+ path: `${path}.${key}`,
416
+ message: "Supported only for \`text-field\`",
417
+ });
398
418
  }
399
- if (!mapboxGlStyleSpec.expression.isExpression(value)) {
400
- if (this.options.verbose) {
401
- console.error("Validate supported concat expression:", "not an expression", key, value, "\n");
402
- }
403
- return false;
419
+ else if (!mapboxGlStyleSpec.expression.isExpression(value)) {
420
+ this.errors.push({
421
+ path: `${path}.${key}`,
422
+ message: "Not an expression",
423
+ });
404
424
  }
405
- const result = value[0] === "concat" &&
406
- value.length > 2 &&
407
- value
408
- .slice(1)
409
- .every(i => typeof i === "string" || (Array.isArray(i) && i[0] === "get" && typeof i[1] === "string"));
410
- if (!result) {
411
- if (this.options.verbose) {
412
- console.error("Validate supported concat expression:", "invalid expression", key, value, "\n");
425
+ else {
426
+ const result = value[0] === "concat" &&
427
+ value.length > 2 &&
428
+ value
429
+ .slice(1)
430
+ .every(i => typeof i === "string" || (Array.isArray(i) && i[0] === "get" && typeof i[1] === "string"));
431
+ if (!result) {
432
+ this.errors.push({
433
+ path: `${path}.${key}`,
434
+ message: "Invalid expression",
435
+ });
413
436
  }
414
437
  }
415
- return result;
438
+ return this.errors.length === errorsCount;
416
439
  }
417
- validateSupportedMatchExpression(key, value) {
440
+ validateSupportedMatchExpression(path, key, value) {
441
+ const errorsCount = this.errors.length;
418
442
  if (!this.getSpecPropValueOptions(key)?.dataExpressionSupport) {
419
- if (this.options.verbose) {
420
- console.error("Validate supported match expression:", "expression not supported", key, value, "\n");
421
- }
422
- return false;
443
+ this.errors.push({
444
+ path: `${path}.${key}`,
445
+ message: "Expression not supported",
446
+ });
423
447
  }
424
- if (!mapboxGlStyleSpec.expression.isExpression(value)) {
425
- if (this.options.verbose) {
426
- console.error("Validate supported match expression:", "not an expression", key, value, "\n");
427
- }
428
- return false;
448
+ else if (!mapboxGlStyleSpec.expression.isExpression(value)) {
449
+ this.errors.push({
450
+ path: `${path}.${key}`,
451
+ message: "Not an expression",
452
+ });
429
453
  }
430
- const result = value[0] === "match" &&
431
- Array.isArray(value[1]) &&
432
- value[1].length === 2 &&
433
- value[1][0] === "get" &&
434
- typeof value[1][1] === "string" &&
435
- value.length > 3 &&
436
- value.length % 2 !== 0 &&
437
- value
438
- .slice(2)
439
- .every(e => typeof e === "number" ||
440
- typeof e === "string" ||
441
- (key === "text-field" && this.validateSupportedConcatExpression(key, e)));
442
- if (!result) {
443
- if (this.options.verbose) {
444
- console.error("Validate supported match expression:", "invalid expression", key, value, "\n");
454
+ else {
455
+ const result = value[0] === "match" &&
456
+ Array.isArray(value[1]) &&
457
+ value[1].length === 2 &&
458
+ value[1][0] === "get" &&
459
+ typeof value[1][1] === "string" &&
460
+ value.length > 3 &&
461
+ value.length % 2 !== 0 &&
462
+ value
463
+ .slice(2)
464
+ .every(e => typeof e === "number" ||
465
+ typeof e === "string" ||
466
+ (key === "text-field" && this.validateSupportedConcatExpression(path, key, e)));
467
+ if (!result) {
468
+ this.errors.push({
469
+ path: `${path}.${key}`,
470
+ message: "Invalid expression",
471
+ });
445
472
  }
446
473
  }
447
- return result;
474
+ return this.errors.length === errorsCount;
448
475
  }
449
- validateSupportedCaseExpression(key, value) {
476
+ validateSupportedCaseExpression(path, key, value) {
477
+ const errorsCount = this.errors.length;
450
478
  if (!this.getSpecPropValueOptions(key)?.dataExpressionSupport) {
451
- if (this.options.verbose) {
452
- console.error("Validate supported case expression:", "expression not supported", key, value, "\n");
453
- }
454
- return false;
479
+ this.errors.push({
480
+ path: `${path}.${key}`,
481
+ message: "Expression not supported",
482
+ });
455
483
  }
456
- if (!mapboxGlStyleSpec.expression.isExpression(value)) {
457
- if (this.options.verbose) {
458
- console.error("Validate supported case expression:", "not an expression", key, value, "\n");
459
- }
460
- return false;
484
+ else if (!mapboxGlStyleSpec.expression.isExpression(value)) {
485
+ this.errors.push({
486
+ path: `${path}.${key}`,
487
+ message: "Not an expression",
488
+ });
461
489
  }
462
- const result = value[0] === "case" &&
463
- value.length > 3 &&
464
- value.length % 2 === 0 &&
465
- value
466
- .slice(1)
467
- .filter((_, i) => i % 2 === 0 && i < value.slice(1).length - 1)
468
- .every(e => Array.isArray(e) &&
469
- e[0] === "all" &&
470
- e.length > 1 &&
471
- e
490
+ else {
491
+ const result = value[0] === "case" &&
492
+ value.length > 3 &&
493
+ value.length % 2 === 0 &&
494
+ value
472
495
  .slice(1)
473
- .every(ce => Array.isArray(ce) &&
474
- ce.length === 3 &&
475
- [">=", ">", "<", "<="].includes(ce[0]) &&
476
- ce[1][0] === "get" &&
477
- typeof ce[1][1] === "string" &&
478
- typeof ce[2] === "number")) &&
479
- value
480
- .slice(1)
481
- .filter((_, i) => i % 2 !== 0)
482
- .every(e => typeof e === "number" ||
483
- typeof e === "string" ||
484
- (key === "text-field" && this.validateSupportedConcatExpression(key, e))) &&
485
- (typeof value[value.slice(1).length - 1] === "number" ||
486
- typeof value[value.slice(1).length - 1] === "string" ||
487
- (key === "text-field" && this.validateSupportedConcatExpression(key, value)));
488
- if (!result) {
489
- if (this.options.verbose) {
490
- console.error("Validate supported case expression:", "invalid expression", key, value, "\n");
496
+ .filter((_, i) => i % 2 === 0 && i < value.slice(1).length - 1)
497
+ .every(e => Array.isArray(e) &&
498
+ e[0] === "all" &&
499
+ e.length > 1 &&
500
+ e
501
+ .slice(1)
502
+ .every(ce => Array.isArray(ce) &&
503
+ ce.length === 3 &&
504
+ [">=", ">", "<", "<="].includes(ce[0]) &&
505
+ ce[1][0] === "get" &&
506
+ typeof ce[1][1] === "string" &&
507
+ typeof ce[2] === "number")) &&
508
+ value
509
+ .slice(1)
510
+ .filter((_, i) => i % 2 !== 0)
511
+ .every(e => typeof e === "number" ||
512
+ typeof e === "string" ||
513
+ (key === "text-field" && this.validateSupportedConcatExpression(path, key, e))) &&
514
+ (typeof value[value.slice(1).length - 1] === "number" ||
515
+ typeof value[value.slice(1).length - 1] === "string" ||
516
+ (key === "text-field" && this.validateSupportedConcatExpression(path, key, value)));
517
+ if (!result) {
518
+ this.errors.push({
519
+ path: `${path}.${key}`,
520
+ message: "Invalid expression",
521
+ });
491
522
  }
492
523
  }
493
- return result;
524
+ return this.errors.length === errorsCount;
494
525
  }
495
- validateSupportedStepExpression(key, value) {
526
+ validateSupportedStepExpression(path, key, value) {
527
+ const errorsCount = this.errors.length;
496
528
  if (!this.getSpecPropValueOptions(key)?.dataExpressionSupport) {
497
- if (this.options.verbose) {
498
- console.error("Validate supported step expression:", "expression not supported", key, value, "\n");
499
- }
500
- return false;
529
+ this.errors.push({
530
+ path: `${path}.${key}`,
531
+ message: "Expression not supported",
532
+ });
501
533
  }
502
- if (!mapboxGlStyleSpec.expression.isExpression(value)) {
503
- if (this.options.verbose) {
504
- console.error("Validate supported step expression:", "not an expression", key, value, "\n");
505
- }
506
- return false;
534
+ else if (!mapboxGlStyleSpec.expression.isExpression(value)) {
535
+ this.errors.push({
536
+ path: `${path}.${key}`,
537
+ message: "Not an expression",
538
+ });
507
539
  }
508
- const result = value[0] === "step" &&
509
- Array.isArray(value[1]) &&
510
- value[1].length === 2 &&
511
- value[1][0] === "get" &&
512
- typeof value[1][1] === "string" &&
513
- value.length > 3 &&
514
- value.length % 2 !== 0 &&
515
- value
516
- .slice(2)
517
- .filter((_, i) => i % 2 === 0)
518
- .every(e => typeof e === "number" ||
519
- typeof e === "string" ||
520
- (key === "text-field" && this.validateSupportedConcatExpression(key, e))) &&
521
- value
522
- .slice(2)
523
- .filter((_, i) => i % 2 !== 0)
524
- .every(e => typeof e === "number");
525
- if (!result) {
526
- if (this.options.verbose) {
527
- console.error("Validate supported step expression:", "invalid expression", key, value, "\n");
540
+ else {
541
+ const result = value[0] === "step" &&
542
+ Array.isArray(value[1]) &&
543
+ value[1].length === 2 &&
544
+ value[1][0] === "get" &&
545
+ typeof value[1][1] === "string" &&
546
+ value.length > 3 &&
547
+ value.length % 2 !== 0 &&
548
+ value
549
+ .slice(2)
550
+ .filter((_, i) => i % 2 === 0)
551
+ .every(e => typeof e === "number" ||
552
+ typeof e === "string" ||
553
+ (key === "text-field" && this.validateSupportedConcatExpression(path, key, e))) &&
554
+ value
555
+ .slice(2)
556
+ .filter((_, i) => i % 2 !== 0)
557
+ .every(e => typeof e === "number");
558
+ if (!result) {
559
+ this.errors.push({
560
+ path: `${path}.${key}`,
561
+ message: "Invalid expression",
562
+ });
528
563
  }
529
564
  }
530
- return result;
565
+ return this.errors.length === errorsCount;
531
566
  }
532
- validateLinearZoomInterpolate(key, value) {
533
- return (this.getSpecPropValueOptions(key)?.interpolateExpressionSupport &&
534
- lodash.isEqual(value[0], "interpolate") &&
535
- lodash.isEqual(value[1], ["linear"]) &&
567
+ validateInterpolateInputs(value) {
568
+ return value
569
+ .slice(3)
570
+ .filter((_, i) => i % 2 === 0)
571
+ .every(e => typeof e === "number");
572
+ }
573
+ validateZoomInterpolateConsistency(path, key, value) {
574
+ return (this.validateInterpolateInputs(value) &&
536
575
  lodash.isEqual(value[2], ["zoom"]) &&
537
576
  value.length % 2 !== 0 &&
538
577
  value.length > 4 &&
539
- value
540
- .slice(3)
541
- .filter((_, i) => i % 2 === 0)
542
- .every(e => typeof e === "number") &&
543
578
  value
544
579
  .slice(3)
545
580
  .filter((_, i) => i % 2 !== 0)
546
581
  .every(e => {
547
582
  return (typeof e === "number" ||
548
583
  typeof e === "string" ||
549
- this.validateSupportedExpression(key, e, true) ||
550
- (key === "text-field" && this.validateSupportedConcatExpression(key, e)));
584
+ this.validateSupportedExpression(path, key, e, true) ||
585
+ (key === "text-field" && this.validateSupportedConcatExpression(path, key, e)));
551
586
  }));
552
587
  }
553
- validateExponentialZoomInterpolate(key, value) {
588
+ validateLinearZoomInterpolate(path, key, value) {
589
+ return (this.getSpecPropValueOptions(key)?.interpolateExpressionSupport &&
590
+ lodash.isEqual(value[0], "interpolate") &&
591
+ lodash.isEqual(value[1], ["linear"]) &&
592
+ this.validateZoomInterpolateConsistency(path, key, value));
593
+ }
594
+ validateExponentialZoomInterpolate(path, key, value) {
554
595
  return (this.getSpecPropValueOptions(key)?.interpolateExpressionSupport &&
555
596
  lodash.isEqual(value[0], "interpolate") &&
556
597
  Array.isArray(value[1]) &&
557
598
  value[1].length === 2 &&
558
599
  value[1][0] === "exponential" &&
559
600
  typeof value[1][1] === "number" &&
560
- lodash.isEqual(value[2], ["zoom"]) &&
561
- value.length % 2 !== 0 &&
562
- value.length > 4 &&
563
- value
564
- .slice(3)
565
- .filter((_, i) => i % 2 === 0)
566
- .every(e => typeof e === "number") &&
567
- value
568
- .slice(3)
569
- .filter((_, i) => i % 2 !== 0)
570
- .every(e => {
571
- return (typeof e === "number" ||
572
- typeof e === "string" ||
573
- this.validateSupportedExpression(key, e, true) ||
574
- (key === "text-field" && this.validateSupportedConcatExpression(key, e)));
575
- }));
601
+ this.validateZoomInterpolateConsistency(path, key, value));
576
602
  }
577
- validateCubicBezierZoomInterpolate(key, value) {
603
+ validateCubicBezierZoomInterpolate(path, key, value) {
578
604
  return (this.getSpecPropValueOptions(key)?.interpolateExpressionSupport &&
579
605
  lodash.isEqual(value[0], "interpolate") &&
580
606
  Array.isArray(value[1]) &&
581
607
  value[1].length === 5 &&
582
608
  value[1][0] === "cubic-bezier" &&
583
609
  value[1].slice(1).every(e => typeof e === "number") &&
584
- lodash.isEqual(value[2], ["zoom"]) &&
585
- value.length % 2 !== 0 &&
586
- value.length > 4 &&
587
- value
588
- .slice(3)
589
- .filter((_, i) => i % 2 === 0)
590
- .every(e => typeof e === "number") &&
591
- value
592
- .slice(3)
593
- .filter((_, i) => i % 2 !== 0)
594
- .every(e => {
595
- return (typeof e === "number" ||
596
- typeof e === "string" ||
597
- this.validateSupportedExpression(key, e, true) ||
598
- (key === "text-field" && this.validateSupportedConcatExpression(key, e)));
599
- }));
610
+ this.validateZoomInterpolateConsistency(path, key, value));
600
611
  }
601
- validateLinearDataInterpolate(key, value) {
602
- return (this.getSpecPropValueOptions(key)?.dataExpressionSupport &&
603
- lodash.isEqual(value[0], "interpolate") &&
604
- lodash.isEqual(value[1], ["linear"]) &&
612
+ validateDataInterpolateConsistency(path, key, value) {
613
+ return (this.validateInterpolateInputs(value) &&
605
614
  Array.isArray(value[2]) &&
606
615
  value[2].length === 2 &&
607
616
  value[2][0] === "get" &&
608
617
  typeof value[2][1] === "string" &&
609
618
  value.length % 2 !== 0 &&
610
619
  value.length > 4 &&
611
- value
612
- .slice(3)
613
- .filter((_, i) => i % 2 === 0)
614
- .every(e => typeof e === "number") &&
615
620
  value
616
621
  .slice(3)
617
622
  .filter((_, i) => i % 2 !== 0)
618
623
  .every(e => typeof e === "number" ||
619
624
  typeof e === "string" ||
620
- (key === "text-field" && this.validateSupportedConcatExpression(key, e))));
625
+ (key === "text-field" && this.validateSupportedConcatExpression(path, key, e))));
626
+ }
627
+ validateLinearDataInterpolate(path, key, value) {
628
+ return (this.getSpecPropValueOptions(key)?.dataExpressionSupport &&
629
+ lodash.isEqual(value[0], "interpolate") &&
630
+ lodash.isEqual(value[1], ["linear"]) &&
631
+ this.validateDataInterpolateConsistency(path, key, value));
621
632
  }
622
- validateExponentialDataInterpolate(key, value) {
633
+ validateExponentialDataInterpolate(path, key, value) {
623
634
  return (this.getSpecPropValueOptions(key)?.dataExpressionSupport &&
624
635
  lodash.isEqual(value[0], "interpolate") &&
625
636
  Array.isArray(value[1]) &&
626
637
  value[1].length === 2 &&
627
638
  value[1][0] === "exponential" &&
628
639
  typeof value[1][1] === "number" &&
629
- Array.isArray(value[2]) &&
630
- value[2].length === 2 &&
631
- value[2][0] === "get" &&
632
- typeof value[2][1] === "string" &&
633
- value.length % 2 !== 0 &&
634
- value.length > 4 &&
635
- value
636
- .slice(3)
637
- .filter((_, i) => i % 2 === 0)
638
- .every(e => typeof e === "number") &&
639
- value
640
- .slice(3)
641
- .filter((_, i) => i % 2 !== 0)
642
- .every(e => typeof e === "number" ||
643
- typeof e === "string" ||
644
- (key === "text-field" && this.validateSupportedConcatExpression(key, e))));
640
+ this.validateDataInterpolateConsistency(path, key, value));
645
641
  }
646
- validateCubicBezierDataInterpolate(key, value) {
642
+ validateCubicBezierDataInterpolate(path, key, value) {
647
643
  return (this.getSpecPropValueOptions(key)?.dataExpressionSupport &&
648
644
  lodash.isEqual(value[0], "interpolate") &&
649
645
  Array.isArray(value[1]) &&
650
646
  value[1].length === 5 &&
651
647
  value[1][0] === "cubic-bezier" &&
652
648
  value[1].slice(1).every(e => typeof e === "number") &&
653
- Array.isArray(value[2]) &&
654
- value[2].length === 2 &&
655
- value[2][0] === "get" &&
656
- typeof value[2][1] === "string" &&
657
- value.length % 2 !== 0 &&
658
- value.length > 4 &&
659
- value
660
- .slice(3)
661
- .filter((_, i) => i % 2 === 0)
662
- .every(e => typeof e === "number") &&
663
- value
664
- .slice(3)
665
- .filter((_, i) => i % 2 !== 0)
666
- .every(e => typeof e === "number" ||
667
- typeof e === "string" ||
668
- (key === "text-field" && this.validateSupportedConcatExpression(key, e))));
669
- }
670
- validateZoomInterpolate(key, value) {
671
- return (this.validateLinearZoomInterpolate(key, value) ||
672
- this.validateExponentialZoomInterpolate(key, value) ||
673
- this.validateCubicBezierZoomInterpolate(key, value));
649
+ this.validateDataInterpolateConsistency(path, key, value));
674
650
  }
675
- validateDataInterpolate(key, value) {
676
- return (this.validateLinearDataInterpolate(key, value) ||
677
- this.validateExponentialDataInterpolate(key, value) ||
678
- this.validateCubicBezierDataInterpolate(key, value));
651
+ validateZoomInterpolate(path, key, value) {
652
+ const isAllowedInterpolate = this.getSpecPropValueOptions(key)?.interpolateExpressionSupport && lodash.isEqual(value[0], "interpolate");
653
+ if (!isAllowedInterpolate) {
654
+ return;
655
+ }
656
+ if (lodash.isEqual(value[1], ["linear"])) {
657
+ this.validateLinearZoomInterpolate(path, key, value);
658
+ }
659
+ if (lodash.isEqual(value[1], ["exponential"])) {
660
+ this.validateExponentialZoomInterpolate(path, key, value);
661
+ }
662
+ if (lodash.isEqual(value[1], ["cubic-bezier"])) {
663
+ this.validateCubicBezierZoomInterpolate(path, key, value);
664
+ }
679
665
  }
680
- validateSupportedInterpolateExpression(key, value, excludeZoomInterpolate) {
681
- if (!mapboxGlStyleSpec.expression.isExpression(value)) {
682
- if (this.options.verbose) {
683
- console.error("Validate supported interpolate expression:", "not an expression", key, value, "\n");
684
- }
685
- return false;
666
+ validateDataInterpolate(path, key, value) {
667
+ const isAllowedInterpolate = this.getSpecPropValueOptions(key)?.dataExpressionSupport && lodash.isEqual(value[0], "interpolate");
668
+ if (!isAllowedInterpolate) {
669
+ return;
686
670
  }
687
- if (key === "heatmap-color") {
688
- const isColorValid = this.isHeatmapColorValid(value);
689
- if (!isColorValid) {
690
- if (this.options.verbose) {
691
- console.error("Validate supported interpolate expression:", "invalid color value", key, value, "\n");
692
- }
693
- }
694
- return this.isHeatmapColorValid(value);
671
+ if (lodash.isEqual(value[1], ["linear"])) {
672
+ this.validateLinearDataInterpolate(path, key, value);
695
673
  }
696
- if (!this.getSpecPropValueOptions(key)?.interpolateExpressionSupport &&
697
- !this.getSpecPropValueOptions(key)?.dataExpressionSupport) {
698
- if (this.options.verbose) {
699
- console.error("Validate supported interpolate expression:", "interpolate not supported", key, value, "\n");
700
- }
674
+ if (lodash.isEqual(value[1], ["exponential"])) {
675
+ this.validateExponentialDataInterpolate(path, key, value);
701
676
  }
702
- const result = (!excludeZoomInterpolate && this.validateZoomInterpolate(key, value)) || this.validateDataInterpolate(key, value);
703
- if (!result) {
704
- if (this.options.verbose) {
705
- console.error("Validate supported interpolate expression:", "invalid expression", key, value, "\n");
706
- }
677
+ if (lodash.isEqual(value[1], ["cubic-bezier"])) {
678
+ this.validateCubicBezierDataInterpolate(path, key, value);
707
679
  }
708
- return result;
709
680
  }
710
- validateSupportedExpression(key, value, excludeZoomInterpolate) {
681
+ validateSupportedInterpolateExpression(path, key, value, excludeZoomInterpolate) {
682
+ const errorsCount = this.errors.length;
683
+ const propValueOptions = this.getSpecPropValueOptions(key);
711
684
  if (!mapboxGlStyleSpec.expression.isExpression(value)) {
712
- if (this.options.verbose) {
713
- console.error("Validate supported expression:", "not an expression", key, value, "\n");
714
- }
715
- return false;
685
+ this.errors.push({
686
+ path: `${path}.${key}`,
687
+ message: "Not an expression",
688
+ });
689
+ }
690
+ else if (key === "heatmap-color" && !this.isHeatmapColorValid(value)) {
691
+ this.errors.push({
692
+ path: `${path}.${key}`,
693
+ message: "Invalid color value",
694
+ });
695
+ }
696
+ else if (!propValueOptions?.interpolateExpressionSupport && !propValueOptions?.dataExpressionSupport) {
697
+ this.errors.push({
698
+ path: `${path}.${key}`,
699
+ message: "Interpolate not supported",
700
+ });
701
+ }
702
+ else if (!excludeZoomInterpolate) {
703
+ this.validateZoomInterpolate(path, key, value);
716
704
  }
717
- const expressionType = value[0];
718
- if (typeof expressionType !== "string" ||
719
- !["match", "case", "step", "interpolate", "concat"].includes(expressionType)) {
720
- if (this.options.verbose) {
721
- console.error("Validate supported expression:", "expression type not supported", key, value, "\n");
722
- }
723
- return false;
705
+ else {
706
+ this.validateDataInterpolate(path, key, value);
724
707
  }
725
- switch (expressionType) {
726
- case "match":
727
- return this.validateSupportedMatchExpression(key, value);
728
- case "case":
729
- return this.validateSupportedCaseExpression(key, value);
730
- case "step":
731
- return this.validateSupportedStepExpression(key, value);
732
- case "concat":
733
- return this.validateSupportedConcatExpression(key, value);
734
- case "interpolate":
735
- return this.validateSupportedInterpolateExpression(key, value, excludeZoomInterpolate);
736
- default:
737
- return false;
708
+ return this.errors.length === errorsCount;
709
+ }
710
+ validateSupportedExpression(path, key, value, excludeZoomInterpolate) {
711
+ const errorsCount = this.errors.length;
712
+ if (!mapboxGlStyleSpec.expression.isExpression(value)) {
713
+ this.errors.push({
714
+ path: `${path}.${key}`,
715
+ message: "Not an expression",
716
+ });
717
+ }
718
+ else {
719
+ const expressionType = value[0];
720
+ if (typeof expressionType !== "string" || !["match", "case", "step", "interpolate", "concat"].includes(expressionType)) {
721
+ this.errors.push({
722
+ path: `${path}.${key}`,
723
+ message: "Expression type not supported (supported types: \"match\", \"case\", \"step\", \"interpolate\", \"concat\")",
724
+ });
725
+ }
726
+ else {
727
+ switch (expressionType) {
728
+ case "match":
729
+ this.validateSupportedMatchExpression(path, key, value);
730
+ break;
731
+ case "case":
732
+ this.validateSupportedCaseExpression(path, key, value);
733
+ break;
734
+ case "step":
735
+ this.validateSupportedStepExpression(path, key, value);
736
+ break;
737
+ case "concat":
738
+ this.validateSupportedConcatExpression(path, key, value);
739
+ break;
740
+ case "interpolate":
741
+ this.validateSupportedInterpolateExpression(path, key, value, excludeZoomInterpolate);
742
+ break;
743
+ }
744
+ }
738
745
  }
746
+ return this.errors.length === errorsCount;
739
747
  }
740
- validateTransitionValue(key, value) {
748
+ validateTransitionValue(path, key, value) {
749
+ const errorsCount = this.errors.length;
741
750
  const propValueOptions = this.getSpecPropValueOptions(key.replace(/-transition$/, ""));
742
751
  if (propValueOptions && propValueOptions.transitionable === false) {
743
- if (this.options.verbose) {
744
- console.error("Validate transition value:", "transition not supported", key, value, "\n");
745
- }
746
- return false;
752
+ this.errors.push({
753
+ path: `${path}.${key}`,
754
+ message: "Transition not supported",
755
+ });
747
756
  }
748
- const result = lodash.isObject(value) &&
749
- (("delay" in value && typeof value.delay === "number") ||
750
- ("duration" in value && typeof value.duration === "number"));
751
- if (!result) {
752
- if (this.options.verbose) {
753
- console.error("Validate transition value:", "invalid transition value", key, value, "\n");
757
+ else {
758
+ const result = lodash.isObject(value) &&
759
+ (("delay" in value && typeof value.delay === "number") ||
760
+ ("duration" in value && typeof value.duration === "number"));
761
+ if (!result) {
762
+ this.errors.push({
763
+ path: `${path}.${key}`,
764
+ message: "Invalid transition value",
765
+ });
754
766
  }
755
767
  }
756
- return result;
768
+ return this.errors.length === errorsCount;
757
769
  }
758
- validateSpecPropValue(key, value) {
770
+ validateSpecPropValue(path, key, value) {
759
771
  if (key.endsWith("-transition")) {
760
- return this.validateTransitionValue(key, value);
772
+ this.validateTransitionValue(path, key, value);
773
+ return;
761
774
  }
762
775
  const propValueOptions = this.getSpecPropValueOptions(key);
776
+ const isExpression = mapboxGlStyleSpec.expression.isExpression(value);
763
777
  if (!propValueOptions) {
764
- return (typeof value === "string" ||
778
+ const result = (typeof value === "string" ||
765
779
  typeof value === "number" ||
766
780
  typeof value === "boolean" ||
767
781
  (Array.isArray(value) && value.every(item => typeof item === "number")) ||
768
782
  (Array.isArray(value) && value.every(item => typeof item === "string")) ||
769
- mapboxGlStyleSpec.expression.isExpression(value));
783
+ isExpression);
784
+ if (!result) {
785
+ this.errors.push({
786
+ path: `${path}.${key}`,
787
+ message: "Invalid value",
788
+ });
789
+ }
790
+ return;
770
791
  }
771
792
  if (propValueOptions.arrayValue) {
772
- return (typeof value === propValueOptions.valueTypeAllowed ||
773
- (Array.isArray(value) && value.every(item => typeof item === propValueOptions.valueTypeAllowed)) ||
774
- this.validateSupportedExpression(key, value));
793
+ if (!propValueOptions.arrayOnly) {
794
+ const result = (typeof value === propValueOptions.valueTypeAllowed ||
795
+ (Array.isArray(value) && value.every(item => typeof item === propValueOptions.valueTypeAllowed)));
796
+ if (!result) {
797
+ this.errors.push({
798
+ path: `${path}.${key}`,
799
+ message: `Only values of type ${propValueOptions.valueTypeAllowed} allowed`,
800
+ });
801
+ }
802
+ }
803
+ else {
804
+ const result = Array.isArray(value) && value.every(item => typeof item === propValueOptions.valueTypeAllowed);
805
+ if (!result) {
806
+ this.errors.push({
807
+ path: `${path}.${key}`,
808
+ message: `Only array of values of type ${propValueOptions.valueTypeAllowed} allowed`,
809
+ });
810
+ }
811
+ }
812
+ return;
813
+ }
814
+ if (isExpression) {
815
+ this.validateSupportedExpression(path, key, value);
816
+ return;
817
+ }
818
+ if (typeof value !== propValueOptions.valueTypeAllowed) {
819
+ this.errors.push({
820
+ path: `${path}.${key}`,
821
+ message: "Invalid value type",
822
+ });
823
+ return;
775
824
  }
776
- return typeof value === propValueOptions.valueTypeAllowed || this.validateSupportedExpression(key, value);
777
825
  }
778
826
  }
779
827