Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions library/src/schemas/literal/literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe('literal', () => {
expectNoSchemaIssue(literal(45.67), [45.67]);
});

test('for NaN literal', () => {
// Regression test for #1529. `literal(NaN)` was an unsatisfiable
// schema because the value check used `===`, and `NaN === NaN` is
// false. After switching to `Object.is`, `literal(NaN)` accepts NaN
// and matches the behavior of `picklist([NaN])`.
expectNoSchemaIssue(literal(NaN), [NaN]);
});

test('for valid string literal', () => {
expectNoSchemaIssue(literal(''), ['']);
expectNoSchemaIssue(literal('foo'), ['foo']);
Expand Down Expand Up @@ -139,6 +147,32 @@ describe('literal', () => {
);
});

test('for invalid NaN literal', () => {
// Regression test for #1529. The fix switched the equality check from
// `===` to `Object.is`, so `literal(NaN)` accepts `NaN` but still
// rejects other numbers (and non-number inputs).
expectSchemaIssue(
literal(NaN, 'message'),
{ ...baseIssue, expected: 'NaN' },
[
123n,
true,
false,
null,
-123,
0,
45.67,
undefined,
'foo',
Symbol(),
{},
[],
// eslint-disable-next-line @typescript-eslint/no-empty-function
() => {},
]
);
});

test('for invalid string literal', () => {
expectSchemaIssue(
literal('123', 'message'),
Expand Down
2 changes: 1 addition & 1 deletion library/src/schemas/literal/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function literal(
return _getStandardProps(this);
},
'~run'(dataset, config) {
if (dataset.value === this.literal) {
if (Object.is(dataset.value, this.literal)) {
// @ts-expect-error
dataset.typed = true;
} else {
Expand Down