Skip to content

Commit d44be6b

Browse files
authored
chore: add regex anchors to validation schemes (#139)
1 parent 1091192 commit d44be6b

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

src/build/tasks/__tests__/theme-json-schema.test.ts

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('validateJson', () => {
8181
},
8282
}),
8383
).toThrowError(
84-
'Tokens validation error: instance.tokens.border-radius-one.$value does not match pattern "\\\\d+(px|rem|%)"',
84+
'Tokens validation error: instance.tokens.border-radius-one.$value does not match pattern "^\\\\d+(\\\\.\\\\d+)?(px|rem|%)$"',
8585
);
8686
});
8787

@@ -158,7 +158,7 @@ describe('validateJson', () => {
158158
},
159159
}),
160160
).toBe(true);
161-
['100 px', '20ps', '100 %'].forEach((invalidValue) => {
161+
['100 px', '20ps', '100 %', '100px a'].forEach((invalidValue) => {
162162
expect(() =>
163163
validateTokens({
164164
'space-button': {
@@ -169,7 +169,7 @@ describe('validateJson', () => {
169169
},
170170
}),
171171
).toThrowError(
172-
'Tokens validation error: instance.tokens.space-button.$value.comfortable does not match pattern "\\\\d+(px|rem|%)"',
172+
'Tokens validation error: instance.tokens.space-button.$value.comfortable does not match pattern "^\\\\d+(\\\\.\\\\d+)?(px|rem|%)$"',
173173
);
174174
});
175175
});
@@ -269,6 +269,62 @@ describe('validateJson', () => {
269269
});
270270
});
271271

272+
describe('font-size', () => {
273+
test('accepts certain formats', () => {
274+
['14px', '2rem', '1em', '1.5rem'].forEach((validValue) => {
275+
expect(
276+
validateTokens({
277+
'font-size-body': {
278+
$value: validValue,
279+
},
280+
}),
281+
).toBe(true);
282+
});
283+
});
284+
285+
test('rejects invalid formats', () => {
286+
['14', '14 px', '14ps', '1em a'].forEach((invalidValue) => {
287+
expect(() =>
288+
validateTokens({
289+
'font-size-body': {
290+
$value: invalidValue,
291+
},
292+
}),
293+
).toThrowError(
294+
'Tokens validation error: instance.tokens.font-size-body.$value does not match pattern "^\\\\d+(\\\\.\\\\d+)?(px|rem|em)$"',
295+
);
296+
});
297+
});
298+
});
299+
300+
describe('line-height', () => {
301+
test('accepts certain formats', () => {
302+
['20px', '1.5rem', '1em'].forEach((validValue) => {
303+
expect(
304+
validateTokens({
305+
'line-height-body': {
306+
$value: validValue,
307+
},
308+
}),
309+
).toBe(true);
310+
});
311+
});
312+
313+
test('rejects invalid formats', () => {
314+
['20', '20 px', '20ps', '1em a'].forEach((invalidValue) => {
315+
expect(() =>
316+
validateTokens({
317+
'line-height-body': {
318+
$value: invalidValue,
319+
},
320+
}),
321+
).toThrowError(
322+
'Tokens validation error: instance.tokens.line-height-body.$value does not match pattern "^\\\\d+(\\\\.\\\\d+)?(px|rem|em)$"',
323+
);
324+
});
325+
});
326+
});
327+
272328
describe('letter spacing', () => {
273329
test('accepts positive, negative, and zero values with valid units', () => {
274330
['1px', '-0.5rem', '0em', '0.25px', '-1.5em', '.5px', '-.5rem'].forEach((validValue) => {
@@ -295,15 +351,15 @@ describe('validateJson', () => {
295351
});
296352

297353
test('rejects invalid formats', () => {
298-
['100', '-1', '1.5', '100 px', '20ps', '.px', '-.rem'].forEach((invalidValue) => {
354+
['100', '-1', '1.5', '100 px', '20ps', '.px', '-.rem', 'revert-', '1px test'].forEach((invalidValue) => {
299355
expect(() =>
300356
validateTokens({
301357
'letter-spacing-button': {
302358
$value: invalidValue,
303359
},
304360
}),
305361
).toThrowError(
306-
'Tokens validation error: instance.tokens.letter-spacing-button.$value does not match pattern "normal|inherit|initial|revert|revert-layer|unset|-?\\\\d*\\\\.?\\\\d+(px|rem|em)"',
362+
'Tokens validation error: instance.tokens.letter-spacing-button.$value does not match pattern "^(normal|inherit|initial|revert|revert-layer|unset|-?\\\\d*\\\\.?\\\\d+(px|rem|em))$"',
307363
);
308364
});
309365
});

src/build/tasks/theme-json-schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ const colorValueSchema: GenericSchema = {
2424
};
2525
const spaceValueSchema: GenericSchema = {
2626
type: 'string',
27-
pattern: '\\d+(px|rem|%)',
27+
pattern: '^\\d+(\\.\\d+)?(px|rem|%)$',
2828
};
2929
const borderWidthValueSchema: GenericSchema = {
3030
type: 'string',
31-
pattern: '\\d+(px|rem|em)',
31+
pattern: '^\\d+(\\.\\d+)?(px|rem|em)$',
3232
};
3333
const textSizeValueSchema: GenericSchema = {
3434
type: 'string',
35-
pattern: '\\d+(px|rem|em)',
35+
pattern: '^\\d+(\\.\\d+)?(px|rem|em)$',
3636
};
3737
const textWeightValueSchema: GenericSchema = {
3838
type: 'string',
3939
pattern: '300|400|700|900|normal|bold|light|heavy',
4040
};
4141
const letterSpacingValueSchema: GenericSchema = {
4242
type: 'string',
43-
pattern: 'normal|inherit|initial|revert|revert-layer|unset|-?\\d*\\.?\\d+(px|rem|em)',
43+
pattern: '^(normal|inherit|initial|revert|revert-layer|unset|-?\\d*\\.?\\d+(px|rem|em))$',
4444
};
4545
const durationValueSchema: GenericSchema = { type: 'string', pattern: '\\d+m?s' };
4646

0 commit comments

Comments
 (0)