-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add duration tokens to validation (#749)
- Loading branch information
1 parent
bcef815
commit 0a3384e
Showing
7 changed files
with
113 additions
and
0 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,12 @@ | ||
import {z} from 'zod' | ||
import {baseToken} from './baseToken' | ||
import {referenceValue} from './referenceValue' | ||
import {durationValue} from './durationValue' | ||
import {tokenType} from './tokenType' | ||
|
||
export const durationToken = baseToken | ||
.extend({ | ||
$value: z.union([durationValue, referenceValue]), | ||
$type: tokenType('duration'), | ||
}) | ||
.strict() |
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,58 @@ | ||
import {durationToken} from './durationToken' | ||
|
||
describe('Schema: durationToken', () => { | ||
const validToken = { | ||
$value: '1000ms', | ||
$type: 'duration', | ||
$description: '1000 milliseconds', | ||
} | ||
|
||
it('parses valid duration tokens', () => { | ||
expect(durationToken.safeParse(validToken).success).toStrictEqual(true) | ||
}) | ||
|
||
it('fails on invalid properties', () => { | ||
// additional element | ||
expect( | ||
durationToken.safeParse({ | ||
...validToken, | ||
duration: '1000s', | ||
}).success, | ||
).toStrictEqual(false) | ||
// missing value | ||
expect( | ||
durationToken.safeParse({ | ||
$type: 'duration', | ||
}).success, | ||
).toStrictEqual(false) | ||
// missing type | ||
expect( | ||
durationToken.safeParse({ | ||
$value: '1000ms', | ||
}).success, | ||
).toStrictEqual(false) | ||
}) | ||
|
||
it('fails on wrong type', () => { | ||
// invalid string | ||
expect( | ||
durationToken.safeParse({ | ||
...validToken, | ||
$type: 'motion', | ||
}).success, | ||
).toStrictEqual(false) | ||
// undefined | ||
expect( | ||
durationToken.safeParse({ | ||
...validToken, | ||
$type: undefined, | ||
}).success, | ||
).toStrictEqual(false) | ||
// no type | ||
expect( | ||
durationToken.safeParse({ | ||
$value: '1000ms', | ||
}).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {z} from 'zod' | ||
import {schemaErrorMessage} from '../utilities/schemaErrorMessage' | ||
|
||
export const durationValue = z.string().refine( | ||
duration => /(^[0-9]+ms$)/.test(duration), | ||
val => ({ | ||
message: schemaErrorMessage(`Invalid duration: "${val}"`, `A duration must be a string with an "ms"`), | ||
}), | ||
) |
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,25 @@ | ||
import {durationValue} from './durationValue' | ||
|
||
describe('Schema: durationValue', () => { | ||
it('passes on valid duration values', () => { | ||
expect(durationValue.safeParse('0ms').success).toStrictEqual(true) | ||
expect(durationValue.safeParse('100ms').success).toStrictEqual(true) | ||
expect(durationValue.safeParse('20000ms').success).toStrictEqual(true) | ||
}) | ||
|
||
it('fails on invalid duration values', () => { | ||
expect(durationValue.safeParse(-1).success).toStrictEqual(false) | ||
expect(durationValue.safeParse(0).success).toStrictEqual(false) | ||
expect(durationValue.safeParse(1.1).success).toStrictEqual(false) | ||
expect(durationValue.safeParse('0').success).toStrictEqual(false) | ||
expect(durationValue.safeParse('10').success).toStrictEqual(false) | ||
expect(durationValue.safeParse('10s').success).toStrictEqual(false) | ||
expect(durationValue.safeParse('').success).toStrictEqual(false) | ||
}) | ||
|
||
it('fails on non-string values', () => { | ||
expect(durationValue.safeParse(undefined).success).toStrictEqual(false) | ||
expect(durationValue.safeParse(null).success).toStrictEqual(false) | ||
expect(durationValue.safeParse(1).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 |
---|---|---|
|
@@ -6,6 +6,7 @@ const validTypes = [ | |
'color', | ||
'typography', | ||
'dimension', | ||
'duration', | ||
'border', | ||
'duration', | ||
'shadow', | ||
|