Allowed shorter instanceId strings

This commit is contained in:
Aria Minaei 2021-09-13 19:16:58 +02:00
parent a1712fce4a
commit d887dad5d4
2 changed files with 27 additions and 10 deletions

View file

@ -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,

View file

@ -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 {