From d887dad5d445dec14518622b1c5d55dfadc0b3ec Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Mon, 13 Sep 2021 19:16:58 +0200 Subject: [PATCH] Allowed shorter instanceId strings --- theatre/core/src/projects/TheatreProject.ts | 4 +-- theatre/shared/src/utils/sanitizers.ts | 33 ++++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/theatre/core/src/projects/TheatreProject.ts b/theatre/core/src/projects/TheatreProject.ts index 1bfcbf8..c7639e1 100644 --- a/theatre/core/src/projects/TheatreProject.ts +++ b/theatre/core/src/projects/TheatreProject.ts @@ -2,7 +2,7 @@ import {privateAPI, setPrivateAPI} from '@theatre/core/privateAPIs' import Project from '@theatre/core/projects/Project' import type {ISheet} from '@theatre/core/sheets/TheatreSheet' import type {ProjectAddress} from '@theatre/shared/utils/addresses' -import {validateName} from '@theatre/shared/utils/sanitizers' +import {validateInstanceId} from '@theatre/shared/utils/sanitizers' import {validateAndSanitiseSlashedPathOrThrow} from '@theatre/shared/utils/slashedPaths' import type {$IntentionalAny} from '@theatre/shared/utils/types' @@ -71,7 +71,7 @@ export default class TheatreProject implements IProject { ) if (process.env.NODE_ENV !== 'production') { - validateName( + validateInstanceId( instanceId, 'instanceId in project.sheet(sheetId, instanceId)', true, diff --git a/theatre/shared/src/utils/sanitizers.ts b/theatre/shared/src/utils/sanitizers.ts index 02eaff8..d4d3399 100644 --- a/theatre/shared/src/utils/sanitizers.ts +++ b/theatre/shared/src/utils/sanitizers.ts @@ -1,13 +1,17 @@ import userReadableTypeOfValue from '@theatre/shared/utils/userReadableTypeOfValue' import {InvalidArgumentError} from '@theatre/shared/utils/errors' -const _validateName = (name: string, thingy: string): void | string => { - if (typeof name !== 'string') { - return `${thingy} must be a string. ${userReadableTypeOfValue(name)} given.` - } else if (name.trim().length !== name.length) { - return `${thingy} must not have leading or trailing spaces. '${name}' given.` - } else if (name.length < 3 || name.length > 32) { - return `${thingy} must have between 3 and 32 characters. '${name}' given.` +const _validateSym = ( + val: string, + thingy: string, + range: [min: number, max: number], +): void | string => { + if (typeof val !== 'string') { + return `${thingy} must be a string. ${userReadableTypeOfValue(val)} given.` + } else if (val.trim().length !== val.length) { + return `${thingy} must not have leading or trailing spaces. '${val}' given.` + } else if (val.length < range[0] || val.length > range[1]) { + return `${thingy} must have between ${range[0]} and ${range[1]} characters. '${val}' given.` } } @@ -16,7 +20,20 @@ export const validateName = ( thingy: string, shouldThrow: boolean = false, ) => { - const result = _validateName(name, thingy) + const result = _validateSym(name, thingy, [3, 32]) + if (typeof result === 'string' && shouldThrow) { + throw new InvalidArgumentError(result) + } else { + return result + } +} + +export const validateInstanceId = ( + name: string, + thingy: string, + shouldThrow: boolean = false, +) => { + const result = _validateSym(name, thingy, [1, 32]) if (typeof result === 'string' && shouldThrow) { throw new InvalidArgumentError(result) } else {