-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add new parser that checks type first * implement better messages
- Loading branch information
1 parent
0490bfc
commit 3532f60
Showing
16 changed files
with
183 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
/** | ||
* Recursively walk a directory and return all file paths | ||
* @param dir directory to search | ||
* @param ignoreDirs directories to ignore | ||
* @returns array of file paths | ||
*/ | ||
export const walkDir = (dir: string, ignoreDirs: string[] = []): string[] => { | ||
const files = fs | ||
.readdirSync(dir, {withFileTypes: true}) | ||
.flatMap(file => { | ||
if (!file.isDirectory()) return path.join(dir, file.name) | ||
if (!ignoreDirs.includes(file.name)) return walkDir(path.join(dir, file.name), ignoreDirs) | ||
}) | ||
.filter(Boolean) as string[] | ||
|
||
return files.flat() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import {z} from 'zod' | ||
import {schemaErrorMessage} from '../utilities/schemaErrorMessage' | ||
|
||
export const alphaValue = z.any().refine( | ||
value => typeof value === 'number' && value >= 0 && value <= 1, | ||
value => ({ | ||
message: `Invalid alpha value: "${value}" (${typeof value}). Alpha value must be a number between 0 and 1."`, | ||
message: schemaErrorMessage( | ||
`Invalid alpha value: "${value}" (${typeof value})`, | ||
'Alpha value must be a number between 0 and 1.', | ||
), | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import {z} from 'zod' | ||
import {schemaErrorMessage} from '../utilities/schemaErrorMessage' | ||
|
||
const colorHex3RegEx = '^#[0-9a-f]{3}$' | ||
const colorHex6RegEx = '^#[0-9a-f]{6}$' | ||
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: schemaErrorMessage( | ||
`Invalid color: ${color}`, | ||
'Color must be a hex string or a reference to a color token.', | ||
), | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import {z} from 'zod' | ||
import {schemaErrorMessage} from '../utilities/schemaErrorMessage' | ||
|
||
export const dimensionValue = z.union([ | ||
z.string().regex(/(^-?[0-9]+(px|rem)$|^-?[0-9]+\.?[0-9]*em$)/, { | ||
message: `Dimension must be a string with a unit (px, rem or em) or 0`, | ||
}), | ||
z.string().refine( | ||
dim => /(^-?[0-9]+(px|rem)$|^-?[0-9]+\.?[0-9]*em$)/.test(dim), | ||
val => ({ | ||
message: schemaErrorMessage( | ||
`Invalid dimension: "${val}"`, | ||
`Dimension must be a string with a unit (px, rem or em) or 0`, | ||
), | ||
}), | ||
), | ||
z.literal('0'), | ||
z.literal(0), | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import {z} from 'zod' | ||
import {schemaErrorMessage} from '../utilities/schemaErrorMessage' | ||
|
||
const allowed = [100, 200, 300, 400, 500, 600, 700, 800, 900, 950] | ||
export const fontWeightValue = z.number().refine( | ||
value => allowed.includes(value), | ||
value => ({message: `Invalid font weight value "${value}", must be one of ${allowed.join(', ')}`}), | ||
value => ({ | ||
message: schemaErrorMessage( | ||
`Invalid font weight value: "${value}"`, | ||
`Font weight must be one of ${allowed.join(', ')}`, | ||
), | ||
}), | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
import {z} from 'zod' | ||
import {schemaErrorMessage} from '../utilities/schemaErrorMessage' | ||
|
||
export const referenceValue = z.string().regex(/^{\w+\.(\w+\.)*\w+}$/) | ||
export const referenceValue = z.string().refine( | ||
ref => /^{\w+\.(\w+\.)*\w+}$/.test(ref), | ||
ref => ({ | ||
message: schemaErrorMessage( | ||
`Invalid reference: "${ref}"`, | ||
'Reference must be a string in the format "{path.to.token}".', | ||
), | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {designToken} from './designToken' | ||
|
||
describe('Schema validation', () => { | ||
const validTokenJson = { | ||
parent: { | ||
color: { | ||
$value: '#000000', | ||
$type: 'color', | ||
}, | ||
}, | ||
} | ||
|
||
it('returns success on valid schema', () => { | ||
const parsedToken = designToken.safeParse(validTokenJson) | ||
expect(parsedToken.success).toStrictEqual(true) | ||
}) | ||
|
||
it('returns success false on invalid schema', () => { | ||
const parsedToken = designToken.safeParse({ | ||
color: { | ||
$value: '#000000', | ||
$type: 'colors', | ||
}, | ||
}) | ||
expect(parsedToken.success).toStrictEqual(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import {z} from 'zod' | ||
import {schemaErrorMessage} from '../utilities/schemaErrorMessage' | ||
|
||
export const tokenName = z.string().regex(/^[a-z0-9][A-Za-z0-9-]*$/, { | ||
message: | ||
'Invalid token 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.', | ||
), | ||
}), | ||
) |
Oops, something went wrong.