Skip to content

Commit

Permalink
add errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Jul 24, 2023
1 parent d149f51 commit 239db35
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
22 changes: 18 additions & 4 deletions scripts/validateTokenJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export const validateTokens = (tokenDir: string) => {
if (!validateTypes.success) {
failed.push({
fileName: file,
errorMessage: fromZodError(validateTypes.error).message.replace(/;/g, '\n- '),
errorMessage: fromZodError(validateTypes.error, {prefix: '', prefixSeparator: '- '}).message.replace(
/;/g,
'\n- ',
),
errors: fromZodError(validateTypes.error).details,
errorsByPath: fromZodError(validateTypes.error).details.reduce((acc, item) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -44,7 +47,10 @@ export const validateTokens = (tokenDir: string) => {
if (validatedTokenJson.success === false) {
failed.push({
fileName: file,
errorMessage: fromZodError(validatedTokenJson.error).message.replace(/;/g, '\n- '),
errorMessage: fromZodError(validatedTokenJson.error, {prefix: '', prefixSeparator: '- '}).message.replace(
/;/g,
'\n- ',
),
errors: fromZodError(validatedTokenJson.error).details,
errorsByPath: fromZodError(validatedTokenJson.error).details.reduce((acc, item) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -94,7 +100,9 @@ if (getFlag('--silent') === null) {
// eslint-disable-next-line no-console
console.log(`\u001b[36;1m\u001b[1m${fail.fileName}\u001b[0m`)
// eslint-disable-next-line no-console
console.log(fail.errorMessage)
console.log(
fail.errorMessage.replace(/\*\*(.*?)\*\*/g, '\u001b[31;1m\u001b[1m$1\u001b[0m').replace(/\n/g, '\n ↳ '),
)
// eslint-disable-next-line no-console
console.log('\n\n')
}
Expand All @@ -112,5 +120,11 @@ if (getFlag('--failOnErrors')) {
if (getFlag('--outFile')) {
// get file name from flag and add .json extension if missing
const filename = `${`${getFlag('--outFile')}`.replace('.json', '')}.json`
fs.writeFileSync(filename, JSON.stringify(failed))
// replace linebreak with <br> for html output
const htmlFailed = failed.map(item => ({
...item,
errorsByPath: JSON.parse(JSON.stringify(item.errorsByPath).replace(/\\n/g, '<br />')),
}))
//
fs.writeFileSync(filename, JSON.stringify(htmlFailed))
}
9 changes: 6 additions & 3 deletions src/schemas/colorHexValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const colorHex8RegEx = '^#[0-9a-f]{8}$'

const colorHexRegex = new RegExp(`(${colorHex3RegEx})|(${colorHex6RegEx})|(${colorHex8RegEx})`, 'i')

export const colorHexValue = z
.string()
.regex(colorHexRegex, {message: 'Invalid color: Color must be a hex string or a reference to a color token.'})
export const colorHexValue = z.string().refine(
color => colorHexRegex.test(color),
color => ({
message: `**Invalid color:** "${color}"\nColor must be a hex string or a reference to a color token.`,
}),
)
2 changes: 1 addition & 1 deletion src/schemas/validTokenType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const validateType = z.record(
$type: z.string().refine(
type => validTypes.includes(type as TokenType),
val => ({
message: `Invalid token $type: "${val}", must be one of the following: ${joinFriendly(
message: `**Invalid token $type:** "${val}"\nMust be one of the following: ${joinFriendly(
[...validTypes],
'or',
)}`,
Expand Down
2 changes: 1 addition & 1 deletion src/tokens/base/color/dark/dark.dimmed.json5
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
color: {
black: {
$value: '#1c2128',
$type: 'color',
$type: 'colors',
$extensions: {
'org.primer.figma': {
collection: 'base/color/dark-dimmed',
Expand Down
4 changes: 2 additions & 2 deletions src/tokens/base/color/dark/dark.high-contrast.json5
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
base: {
color: {
black: {
$value: '#010409',
$value: '#010409SS',
$type: 'color',
},
white: {
$value: '#ffffff',
$value: '#ffffffSS',
$type: 'color',
},
gray: {
Expand Down

0 comments on commit 239db35

Please sign in to comment.