|
1 | 1 | import {z} from 'zod'
|
2 | 2 | import {schemaErrorMessage} from '../utilities/schemaErrorMessage'
|
3 | 3 |
|
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