Skip to content

Commit fe5f8cb

Browse files
committed
feat(sandbox): move validation to a better place -> manifest.ts
1 parent 5deacfd commit fe5f8cb

File tree

3 files changed

+46
-52
lines changed

3 files changed

+46
-52
lines changed

packages/field-plugin/helpers/vite/src/manifest.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { existsSync, readFileSync } from 'fs'
22
import { resolve } from 'path'
3+
import { isEmpty, isOptionObject, isString } from './validation/manifest.ts'
34

45
export const MANIFEST_FILE_NAME = 'field-plugin.config.json'
56
export const MANIFEST_FULL_PATH = resolve(process.cwd(), MANIFEST_FILE_NAME)
@@ -48,12 +49,40 @@ const validateSchema = (manifest: Manifest): void => {
4849
throw new Error(`When declared, the 'options' property should be an array`)
4950
}
5051

51-
manifest.options?.forEach((option) => {
52-
if (!('name' in option && 'value' in option)) {
53-
throw new Error(
54-
`Some of the defined 'options' are invalid. ` +
55-
`Please, make sure they contain a 'name' and 'value' properties`,
52+
validateOptions(manifest.options)
53+
return
54+
}
55+
56+
const validateOptions = (options: unknown[]): void => {
57+
const incorrectValues: string[] = []
58+
59+
for (const option of options) {
60+
if (!isOptionObject(option)) {
61+
incorrectValues.push(
62+
`${JSON.stringify(option)} --> Incorrect object value. Must be of type {"name": string, "value": string}.`,
5663
)
64+
continue
5765
}
58-
})
66+
67+
if (!isString(option.value)) {
68+
incorrectValues.push(
69+
`${JSON.stringify(option)} --> Incorrect value type. Must be of type string.`,
70+
)
71+
continue
72+
}
73+
74+
if (isEmpty(option.value)) {
75+
incorrectValues.push(
76+
`${JSON.stringify(option)} --> Incorrect value. Must not be empty.`,
77+
)
78+
continue
79+
}
80+
}
81+
82+
if (incorrectValues.length > 0) {
83+
throw new Error(
84+
'ERROR: Each option must be an object with string properties "name" and "value". The following values need to be corrected: \n ' +
85+
incorrectValues.join('\n '),
86+
)
87+
}
5988
}

packages/field-plugin/helpers/vite/src/sandbox.ts

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { bold, red } from './utils/text'
33
import { arrows } from './utils/arrows'
44
import { load, manifestExists, MANIFEST_FILE_NAME } from './manifest'
55
import type { Manifest } from './manifest'
6-
import { isFieldPluginOption } from '@storyblok/field-plugin'
76

87
export const SANDBOX_BASE_URL = `https://plugin-sandbox.storyblok.com/field-plugin`
98

@@ -12,47 +11,6 @@ export type SandboxQueryParams = {
1211
manifest: Manifest | null
1312
}
1413

15-
const areOptionsValid = (options: unknown): boolean => {
16-
if (!Array.isArray(options) || options.length === 0) {
17-
displayManifestErrorLoading(
18-
new Error('ERROR: Manifest options must be an array of objects'),
19-
)
20-
return false
21-
}
22-
23-
const incorrectValues: string[] = []
24-
25-
for (const option of options) {
26-
if (!isFieldPluginOption(option)) {
27-
incorrectValues.push(JSON.stringify(option))
28-
}
29-
}
30-
31-
if (incorrectValues.length > 0) {
32-
displayManifestErrorLoading(
33-
new Error(
34-
'ERROR: Each option must be an object with string properties "name" and "value". The following values need to be corrected: \n ' +
35-
incorrectValues.join(',\n '),
36-
),
37-
)
38-
return false
39-
}
40-
41-
return true
42-
}
43-
44-
const isManifestValid = (manifest: unknown): boolean => {
45-
if (typeof manifest !== 'object' || manifest === null) {
46-
return false
47-
}
48-
49-
if ('options' in manifest && manifest.options !== null) {
50-
return areOptionsValid(manifest.options)
51-
}
52-
53-
return true
54-
}
55-
5614
export const buildQueryString = (params: SandboxQueryParams) => {
5715
const queryParams: querystring.ParsedUrlQueryInput = {
5816
url: params.url,
@@ -62,10 +20,6 @@ export const buildQueryString = (params: SandboxQueryParams) => {
6220
return querystring.stringify(queryParams)
6321
}
6422

65-
if (!isManifestValid(params.manifest)) {
66-
throw Error('Invalid manifest')
67-
}
68-
6923
queryParams.manifest = JSON.stringify(params.manifest)
7024
}
7125

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ManifestOption } from '../manifest.ts'
2+
3+
export const isString = (value: unknown) => typeof value === 'string'
4+
5+
export const isEmpty = (value: string) => value === ''
6+
7+
export const isOptionObject = (option: unknown): option is ManifestOption =>
8+
typeof option === 'object' &&
9+
option !== null &&
10+
'name' in option &&
11+
'value' in option

0 commit comments

Comments
 (0)