-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug(core): fix issue with date formatting in validation message (#5551)
* bug(i18n): fix issue with date formatting in validation message * bug(i18n): better fix with tests * bug(i18n): fix date drift on validation and date validator tests * fix(core): fix type issues and add tests for datetime input (#5558) * fix(core): update date validator tests to use strings * chore(core): use centrally defined date constants --------- Co-authored-by: Binoy Patel <[email protected]>
- Loading branch information
Showing
7 changed files
with
245 additions
and
42 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
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
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
105 changes: 105 additions & 0 deletions
105
packages/sanity/test/validation/__snapshots__/dates.test.ts.snap
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,105 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`date with custom format max length constraint: Must be at or before 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or before 01-01-2024", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or before 01-01-2024", | ||
"path": Array [], | ||
}, | ||
] | ||
`; | ||
|
||
exports[`date with custom format min length constraint: Must be at or after 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or after 01-01-2024", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or after 01-01-2024", | ||
"path": Array [], | ||
}, | ||
] | ||
`; | ||
|
||
exports[`date with default format max length constraint: Must be at or before 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or before 2024-01-01", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or before 2024-01-01", | ||
"path": Array [], | ||
}, | ||
] | ||
`; | ||
|
||
exports[`date with default format min length constraint: Must be at or after 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or after 2024-01-01", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or after 2024-01-01", | ||
"path": Array [], | ||
}, | ||
] | ||
`; | ||
|
||
exports[`datetime with custom format max length constraint: Must be at or before 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or before 1st. January 2024 09:31", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or before 1st. January 2024 09:31", | ||
"path": Array [], | ||
}, | ||
] | ||
`; | ||
|
||
exports[`datetime with custom format min length constraint: Must be at or after 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or after 1st. January 2024 09:31", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or after 1st. January 2024 09:31", | ||
"path": Array [], | ||
}, | ||
] | ||
`; | ||
|
||
exports[`datetime with default format max length constraint: Must be at or before 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or before 2024-01-01 09:31", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or before 2024-01-01 09:31", | ||
"path": Array [], | ||
}, | ||
] | ||
`; | ||
|
||
exports[`datetime with default format min length constraint: Must be at or after 1`] = ` | ||
Array [ | ||
Object { | ||
"item": Object { | ||
"message": "Must be at or after 2024-01-01 09:31", | ||
}, | ||
"level": "error", | ||
"message": "Must be at or after 2024-01-01 09:31", | ||
"path": Array [], | ||
}, | ||
] | ||
`; |
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,102 @@ | ||
import {getFallbackLocaleSource} from '../../src/core/i18n/fallback' | ||
import {Rule} from '../../src/core/validation' | ||
|
||
describe('date', () => { | ||
describe('with default format', () => { | ||
const context: any = {client: {}, i18n: getFallbackLocaleSource(), type: {name: 'date'}} | ||
|
||
test('min length constraint', async () => { | ||
const rule = Rule.dateTime().min('2024-01-01') | ||
await expect(rule.validate('2023-12-31', context)).resolves.toMatchSnapshot( | ||
'Must be at or after', | ||
) | ||
await expect(rule.validate('2024-01-02', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01', context)).resolves.toHaveLength(0) | ||
}) | ||
|
||
test('max length constraint', async () => { | ||
const rule = Rule.dateTime().max('2024-01-01') | ||
await expect(rule.validate('2024-01-02', context)).resolves.toMatchSnapshot( | ||
'Must be at or before', | ||
) | ||
await expect(rule.validate('2023-12-31', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01', context)).resolves.toHaveLength(0) | ||
}) | ||
}) | ||
|
||
describe('with custom format', () => { | ||
const context: any = { | ||
client: {}, | ||
i18n: getFallbackLocaleSource(), | ||
type: {name: 'date', options: {dateFormat: 'MM-DD-YYYY'}}, | ||
} | ||
|
||
test('min length constraint', async () => { | ||
const rule = Rule.dateTime().min('2024-01-01') | ||
await expect(rule.validate('2023-12-31', context)).resolves.toMatchSnapshot( | ||
'Must be at or after', | ||
) | ||
await expect(rule.validate('2024-01-02', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01', context)).resolves.toHaveLength(0) | ||
}) | ||
|
||
test('max length constraint', async () => { | ||
const rule = Rule.dateTime().max('2024-01-01') | ||
await expect(rule.validate('2024-01-02', context)).resolves.toMatchSnapshot( | ||
'Must be at or before', | ||
) | ||
await expect(rule.validate('2023-12-31', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01', context)).resolves.toHaveLength(0) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('datetime', () => { | ||
describe('with default format', () => { | ||
const context: any = {client: {}, i18n: getFallbackLocaleSource(), type: {name: 'datetime'}} | ||
|
||
test('min length constraint', async () => { | ||
const rule = Rule.dateTime().min('2024-01-01T17:31:00.000Z') | ||
await expect(rule.validate('2023-12-31T17:31:00.000Z', context)).resolves.toMatchSnapshot( | ||
'Must be at or after', | ||
) | ||
await expect(rule.validate('2024-01-02T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
}) | ||
|
||
test('max length constraint', async () => { | ||
const rule = Rule.dateTime().max('2024-01-01T17:31:00.000Z') | ||
await expect(rule.validate('2024-01-02T17:31:00.000Z', context)).resolves.toMatchSnapshot( | ||
'Must be at or before', | ||
) | ||
await expect(rule.validate('2023-12-23T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
}) | ||
}) | ||
|
||
describe('with custom format', () => { | ||
const context: any = { | ||
client: {}, | ||
i18n: getFallbackLocaleSource(), | ||
type: {name: 'datetime', options: {dateFormat: 'Do. MMMM YYYY'}}, | ||
} | ||
|
||
test('min length constraint', async () => { | ||
const rule = Rule.dateTime().min('2024-01-01T17:31:00.000Z') | ||
await expect(rule.validate('2023-12-31T17:31:00.000Z', context)).resolves.toMatchSnapshot( | ||
'Must be at or after', | ||
) | ||
await expect(rule.validate('2024-01-02T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
}) | ||
|
||
test('max length constraint', async () => { | ||
const rule = Rule.dateTime().max('2024-01-01T17:31:00.000Z') | ||
await expect(rule.validate('2024-01-02T17:31:00.000Z', context)).resolves.toMatchSnapshot( | ||
'Must be at or before', | ||
) | ||
await expect(rule.validate('2023-12-31T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
await expect(rule.validate('2024-01-01T17:31:00.000Z', context)).resolves.toHaveLength(0) | ||
}) | ||
}) | ||
}) |
febdc4b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
performance-studio – ./
performance-studio-git-next.sanity.build
performance-studio.sanity.build
febdc4b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
test-studio – ./
test-studio-git-next.sanity.build
test-studio.sanity.build