Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLL-303: Assert that open, close, high, low, value can safely represe… #1774

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
4 changes: 2 additions & 2 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default [
path: 'dist/lightweight-charts.production.mjs',
import: '{ createSeriesMarkers }',
ignore: ['fancy-canvas'],
limit: '4.08 KB',
limit: '4.5 KB',
brotli: true,
},
{
Expand Down Expand Up @@ -119,7 +119,7 @@ export default [
path: 'dist/lightweight-charts.production.mjs',
import: '{ CandlestickSeries }',
ignore: ['fancy-canvas'],
limit: '2.5 KB',
limit: '3.00 KB',
brotli: true,
},
{
Expand Down
56 changes: 30 additions & 26 deletions src/model/data-validators.ts
SlicedSilver marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { assert } from '../helpers/assertions';

import { isFulfilledData, SeriesDataItemTypeMap } from './data-consumer';
import { isFulfilledData, OhlcData, SeriesDataItemTypeMap } from './data-consumer';
import { IHorzScaleBehavior } from './ihorz-scale-behavior';
import { CreatePriceLineOptions } from './price-line-options';
import { SeriesType } from './series-options';
Expand Down Expand Up @@ -67,31 +67,21 @@ function checkBarItem<HorzScaleItem>(
if (!isFulfilledData(barItem)) {
return;
}

assert(
typeof barItem.open === 'number',
`${type} series item data value of open must be a number, got=${typeof barItem.open}, value=${
barItem.open
}`
);
assert(
typeof barItem.high === 'number',
`${type} series item data value of high must be a number, got=${typeof barItem.high}, value=${
barItem.high
}`
);
assert(
typeof barItem.low === 'number',
`${type} series item data value of low must be a number, got=${typeof barItem.low}, value=${
barItem.low
}`
);
assert(
typeof barItem.close === 'number',
`${type} series item data value of close must be a number, got=${typeof barItem.close}, value=${
barItem.close
}`
);
(['open', 'high', 'low', 'close'] as (keyof OhlcData)[]).forEach((key: keyof OhlcData) => {
assert(
typeof barItem[key] === 'number',
`${type} series item data value of ${key} must be a number, got=${typeof barItem[key]}, value=${
barItem[key]
}`
);

assert(
isSafeValue(barItem[key]),
`${type} series item data value of ${key} must be between ${MIN_SAVE_VALUE.toPrecision(16)} and ${MAX_SAVE_VALUE.toPrecision(16)}, got=${typeof barItem[key]}, value=${
barItem[key]
}`
);
});
}

function checkLineItem<HorzScaleItem>(
Expand All @@ -108,6 +98,13 @@ function checkLineItem<HorzScaleItem>(
lineItem.value
}`
);

assert(
isSafeValue(lineItem.value),
`${type} series item data value must be between ${MIN_SAVE_VALUE.toPrecision(16)} and ${MAX_SAVE_VALUE.toPrecision(16)}, got=${typeof lineItem.value}, value=${
lineItem.value
}`
);
}

function checkCustomItem(
Expand All @@ -117,3 +114,10 @@ function checkCustomItem(
// Nothing to check yet...
return;
}

const MIN_SAVE_VALUE = Number.MIN_SAFE_INTEGER / 100;
const MAX_SAVE_VALUE = Number.MAX_SAFE_INTEGER / 100;
signatenkov marked this conversation as resolved.
Show resolved Hide resolved

function isSafeValue(value: number): boolean {
return value >= MIN_SAVE_VALUE && value <= MAX_SAVE_VALUE;
}
Loading