Skip to content

Commit

Permalink
add linting validation for cubicBezier token schema (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann authored Oct 30, 2024
1 parent 08186e7 commit de32449
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-oranges-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/primitives': patch
---

Add linting validation for cubicBezier token schema
9 changes: 9 additions & 0 deletions src/schemas/cubicBezierToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {z} from 'zod'
import {baseToken} from './baseToken.js'
import {referenceValue} from './referenceValue.js'
import {tokenType} from './tokenType.js'

export const cubicBezierToken = baseToken.extend({
$value: z.union([z.array(z.number()).length(4), referenceValue]),
$type: tokenType('cubicBezier'),
})
22 changes: 22 additions & 0 deletions src/schemas/cubicBezierTokenSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {cubicBezierToken} from './cubicBezierToken.js'

describe('Schema: cubicBezier', () => {
const validToken = {
$value: [0, 0, 1, 1],
$type: 'cubicBezier',
$description: 'a cubicBezier token',
}

it('passes on valid cubicBezier values', () => {
expect(cubicBezierToken.safeParse(validToken).success).toStrictEqual(true)
expect(cubicBezierToken.safeParse({...validToken, $value: [0.2, 0.3, 0.85, 1]}).success).toStrictEqual(true)
})

it('fails on invalid cubicBezier values', () => {
expect(cubicBezierToken.safeParse({...validToken, $value: ['1', 1, 0, 0]}).success).toStrictEqual(false)
expect(cubicBezierToken.safeParse({...validToken, $value: [0, 1]}).success).toStrictEqual(false)
expect(cubicBezierToken.safeParse({...validToken, $value: [0, 1, 1, 0, 0, 1]}).success).toStrictEqual(false)
expect(cubicBezierToken.safeParse({...validToken, $value: 1}).success).toStrictEqual(false)
expect(cubicBezierToken.safeParse({...validToken, $value: 'linear'}).success).toStrictEqual(false)
})
})
2 changes: 2 additions & 0 deletions src/schemas/designToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {colorToken} from './colorToken.js'
import {fontFamilyToken} from './fontFamilyToken.js'
import {shadowToken} from './shadowToken.js'
import {durationToken} from './durationToken.js'
import {cubicBezierToken} from './cubicBezierToken.js'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: TODO: fix this
Expand All @@ -20,6 +21,7 @@ export const designToken = z.record(
return z.union([
z.discriminatedUnion('$type', [
colorToken,
cubicBezierToken,
dimensionToken,
shadowToken,
borderToken,
Expand Down
1 change: 1 addition & 0 deletions src/schemas/validTokenType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {joinFriendly, schemaErrorMessage} from '../utilities/index.js'

const validTypes = [
'color',
'cubicBezier',
'typography',
'dimension',
'duration',
Expand Down

0 comments on commit de32449

Please sign in to comment.