From 8f1d26536b0b933b20b75423ec6f99cf6c37c6b1 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Fri, 8 Jul 2022 13:58:03 +0200 Subject: [PATCH] Sanitize sequence's `length` and `subUnitsPerUnit` Possibly fixes P-173 --- theatre/core/src/sheets/Sheet.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/theatre/core/src/sheets/Sheet.ts b/theatre/core/src/sheets/Sheet.ts index dc06b39..9d8ead8 100644 --- a/theatre/core/src/sheets/Sheet.ts +++ b/theatre/core/src/sheets/Sheet.ts @@ -9,6 +9,7 @@ import type SheetTemplate from './SheetTemplate' import type {ObjectAddressKey, SheetInstanceId} from '@theatre/shared/utils/ids' import type {StrictRecord} from '@theatre/shared/utils/types' import type {ILogger} from '@theatre/shared/logger' +import {isInteger} from 'lodash-es' type SheetObjectMap = StrictRecord @@ -77,12 +78,12 @@ export default class Sheet { const lengthD = valueDerivation( this.project.pointers.historic.sheetsById[this.address.sheetId].sequence .length, - ).map((s) => (typeof s === 'number' ? s : 10)) + ).map(sanitizeSequenceLength) const subUnitsPerUnitD = valueDerivation( this.project.pointers.historic.sheetsById[this.address.sheetId].sequence .subUnitsPerUnit, - ).map((s) => (typeof s === 'number' ? s : 30)) + ).map(sanitizeSequenceSubUnitsPerUnit) this._sequence = new Sequence( this.template.project, @@ -94,3 +95,11 @@ export default class Sheet { return this._sequence } } + +const sanitizeSequenceLength = (len: number | undefined): number => + typeof len === 'number' && isFinite(len) && len > 0 ? len : 10 + +const sanitizeSequenceSubUnitsPerUnit = (subs: number | undefined): number => + typeof subs === 'number' && isInteger(subs) && subs >= 1 && subs <= 1000 + ? subs + : 30