Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prototype to show how we can validate token names #996

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/schemas/tokenName.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import {z} from 'zod'
import {schemaErrorMessage} from '../utilities/schemaErrorMessage'

export const tokenName = z.string().refine(
name => /(^[a-z0-9][A-Za-z0-9-]*$|^@$)/.test(name),
name => ({
message: schemaErrorMessage(
`Invalid token name: "${name}"`,
'Token name must be kebab-case or camelCase, and start with a lowercase letter or number and consist only of letters, numbers, and hyphens.',
),
}),
)
export const tokenName = z
.string()
.refine(
name => /(^[a-z0-9][A-Za-z0-9-]*$|^@$)/.test(name),
name => ({
message: schemaErrorMessage(
`Invalid token name: "${name}"`,
'Token name must be kebab-case or camelCase, and start with a lowercase letter or number and consist only of letters, numbers, and hyphens.',
),
}),
)
.superRefine((val, ctx) => {
// validate base tokens
const name = ctx.path.join('-')
if (ctx.path[0] === 'base' && /(^base-[text|color|size]-[A-Za-z0-9-]*$)/.test(name)) {
ctx.addIssue({
code: z.ZodIssueCode.invalid_string,
validation: 'regex',
message: schemaErrorMessage(
`Invalid base token name: "${name}"`,
'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.',
),
})
}
})
Loading