Skip to content

Commit 82ab3ad

Browse files
prototype to show how we can validate token names
1 parent 9da1712 commit 82ab3ad

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/schemas/tokenName.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import {z} from 'zod'
22
import {schemaErrorMessage} from '../utilities/schemaErrorMessage'
33

4-
export const tokenName = z.string().refine(
5-
name => /(^[a-z0-9][A-Za-z0-9-]*$|^@$)/.test(name),
6-
name => ({
7-
message: schemaErrorMessage(
8-
`Invalid token name: "${name}"`,
9-
'Token name must be kebab-case or camelCase, and start with a lowercase letter or number and consist only of letters, numbers, and hyphens.',
10-
),
11-
}),
12-
)
4+
export const tokenName = z
5+
.string()
6+
.refine(
7+
name => /(^[a-z0-9][A-Za-z0-9-]*$|^@$)/.test(name),
8+
name => ({
9+
message: schemaErrorMessage(
10+
`Invalid token name: "${name}"`,
11+
'Token name must be kebab-case or camelCase, and start with a lowercase letter or number and consist only of letters, numbers, and hyphens.',
12+
),
13+
}),
14+
)
15+
.superRefine((val, ctx) => {
16+
// validate base tokens
17+
const name = ctx.path.join('-')
18+
if (ctx.path[0] === 'base' && /(^base-[text|color|size]-[A-Za-z0-9-]*$)/.test(name)) {
19+
ctx.addIssue({
20+
code: z.ZodIssueCode.invalid_string,
21+
validation: 'regex',
22+
message: schemaErrorMessage(
23+
`Invalid base token name: "${name}"`,
24+
'Base token names must be kebab-case or camelCase, and start with a lowercase letter or number and consist only of letters, numbers, and hyphens.',
25+
),
26+
})
27+
}
28+
})

0 commit comments

Comments
 (0)