@naturalcycles/nodejs-lib 15.85.0 → 15.86.0
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _isBetween, _lazyValue } from '@naturalcycles/js-lib';
|
|
1
|
+
import { _isBetween, _lazyValue, _round } from '@naturalcycles/js-lib';
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib/error';
|
|
3
3
|
import { _deepCopy, _mapObject, Set2 } from '@naturalcycles/js-lib/object';
|
|
4
4
|
import { _substringAfterLast } from '@naturalcycles/js-lib/string';
|
|
@@ -560,6 +560,20 @@ export function createAjv(opt) {
|
|
|
560
560
|
return validate;
|
|
561
561
|
},
|
|
562
562
|
});
|
|
563
|
+
ajv.addKeyword({
|
|
564
|
+
keyword: 'precision',
|
|
565
|
+
type: ['number'],
|
|
566
|
+
modifying: true,
|
|
567
|
+
errors: false,
|
|
568
|
+
schemaType: 'number',
|
|
569
|
+
validate: function validate(numberOfDigits, data, _schema, ctx) {
|
|
570
|
+
if (!numberOfDigits)
|
|
571
|
+
return true;
|
|
572
|
+
_assert(ctx?.parentData && ctx.parentDataProperty !== undefined, 'You should only use `precision(n) on a property of an object, or on an element of an array due to Ajv mutation issues.');
|
|
573
|
+
ctx.parentData[ctx.parentDataProperty] = _round(data, 10 ** (-1 * numberOfDigits));
|
|
574
|
+
return true;
|
|
575
|
+
},
|
|
576
|
+
});
|
|
563
577
|
return ajv;
|
|
564
578
|
}
|
|
565
579
|
const monthLengths = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
@@ -279,6 +279,12 @@ export declare class JsonSchemaNumberBuilder<IN extends number | undefined = num
|
|
|
279
279
|
unixTimestamp2000Millis(): JsonSchemaNumberBuilder<UnixTimestampMillis, UnixTimestampMillis, Opt>;
|
|
280
280
|
utcOffset(): this;
|
|
281
281
|
utcOffsetHour(): this;
|
|
282
|
+
/**
|
|
283
|
+
* Specify the precision of the floating point numbers by the number of digits after the ".".
|
|
284
|
+
* Excess digits will be cut-off when the current schema is nested in an object or array schema,
|
|
285
|
+
* due to how mutability works in Ajv.
|
|
286
|
+
*/
|
|
287
|
+
precision(numberOfDigits: number): this;
|
|
282
288
|
}
|
|
283
289
|
export declare class JsonSchemaBooleanBuilder<IN extends boolean | undefined = boolean, OUT = IN, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
|
|
284
290
|
constructor();
|
|
@@ -512,6 +518,7 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
|
|
|
512
518
|
schemaDictionary: Record<string, JsonSchema>;
|
|
513
519
|
};
|
|
514
520
|
anyOfThese?: JsonSchema[];
|
|
521
|
+
precision?: number;
|
|
515
522
|
}
|
|
516
523
|
declare function object(props: AnyObject): never;
|
|
517
524
|
declare function object<IN extends AnyObject>(props: {
|
|
@@ -632,6 +632,14 @@ export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
|
|
|
632
632
|
utcOffsetHour() {
|
|
633
633
|
return this.integer().min(-12).max(14);
|
|
634
634
|
}
|
|
635
|
+
/**
|
|
636
|
+
* Specify the precision of the floating point numbers by the number of digits after the ".".
|
|
637
|
+
* Excess digits will be cut-off when the current schema is nested in an object or array schema,
|
|
638
|
+
* due to how mutability works in Ajv.
|
|
639
|
+
*/
|
|
640
|
+
precision(numberOfDigits) {
|
|
641
|
+
return this.cloneAndUpdateSchema({ precision: numberOfDigits });
|
|
642
|
+
}
|
|
635
643
|
}
|
|
636
644
|
export class JsonSchemaBooleanBuilder extends JsonSchemaAnyBuilder {
|
|
637
645
|
constructor() {
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _isBetween, _lazyValue } from '@naturalcycles/js-lib'
|
|
1
|
+
import { _isBetween, _lazyValue, _round } from '@naturalcycles/js-lib'
|
|
2
2
|
import { _assert } from '@naturalcycles/js-lib/error'
|
|
3
3
|
import { _deepCopy, _mapObject, Set2 } from '@naturalcycles/js-lib/object'
|
|
4
4
|
import { _substringAfterLast } from '@naturalcycles/js-lib/string'
|
|
@@ -639,6 +639,26 @@ export function createAjv(opt?: Options): Ajv2020 {
|
|
|
639
639
|
},
|
|
640
640
|
})
|
|
641
641
|
|
|
642
|
+
ajv.addKeyword({
|
|
643
|
+
keyword: 'precision',
|
|
644
|
+
type: ['number'],
|
|
645
|
+
modifying: true,
|
|
646
|
+
errors: false,
|
|
647
|
+
schemaType: 'number',
|
|
648
|
+
validate: function validate(numberOfDigits: number, data: number, _schema, ctx) {
|
|
649
|
+
if (!numberOfDigits) return true
|
|
650
|
+
|
|
651
|
+
_assert(
|
|
652
|
+
ctx?.parentData && ctx.parentDataProperty !== undefined,
|
|
653
|
+
'You should only use `precision(n) on a property of an object, or on an element of an array due to Ajv mutation issues.',
|
|
654
|
+
)
|
|
655
|
+
|
|
656
|
+
ctx.parentData[ctx.parentDataProperty] = _round(data, 10 ** (-1 * numberOfDigits))
|
|
657
|
+
|
|
658
|
+
return true
|
|
659
|
+
},
|
|
660
|
+
})
|
|
661
|
+
|
|
642
662
|
return ajv
|
|
643
663
|
}
|
|
644
664
|
|
|
@@ -888,6 +888,15 @@ export class JsonSchemaNumberBuilder<
|
|
|
888
888
|
utcOffsetHour(): this {
|
|
889
889
|
return this.integer().min(-12).max(14)
|
|
890
890
|
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* Specify the precision of the floating point numbers by the number of digits after the ".".
|
|
894
|
+
* Excess digits will be cut-off when the current schema is nested in an object or array schema,
|
|
895
|
+
* due to how mutability works in Ajv.
|
|
896
|
+
*/
|
|
897
|
+
precision(numberOfDigits: number): this {
|
|
898
|
+
return this.cloneAndUpdateSchema({ precision: numberOfDigits })
|
|
899
|
+
}
|
|
891
900
|
}
|
|
892
901
|
|
|
893
902
|
export class JsonSchemaBooleanBuilder<
|
|
@@ -1572,6 +1581,7 @@ export interface JsonSchema<IN = unknown, OUT = IN> {
|
|
|
1572
1581
|
schemaDictionary: Record<string, JsonSchema>
|
|
1573
1582
|
}
|
|
1574
1583
|
anyOfThese?: JsonSchema[]
|
|
1584
|
+
precision?: number
|
|
1575
1585
|
}
|
|
1576
1586
|
|
|
1577
1587
|
function object(props: AnyObject): never
|