-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dateFrom, dateTo validation to CalendarDay (#3002)
* Add dateFrom, dateTo validation to CalendarDay
- Loading branch information
Showing
17 changed files
with
236 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
'@keystonejs/api-tests': patch | ||
'@keystonejs/demo-project-blog': patch | ||
'@keystonejs/demo-project-meetup': patch | ||
'@arch-ui/day-picker': patch | ||
'@keystonejs/fields': major | ||
--- | ||
|
||
The `CalendarDay` field type options `yearRangeFrom` and `yearRangeTo` have been removed, and replaced with `dateFrom` and `dateTo`. These options take an ISO8601 formatted date string in the form `YYYY-MM-DD`, e.g. `2020-06-30`. These values are now validated on the server-side, rather than just on the client-side within the Admin UI. | ||
|
||
If you are currently using `yearRangeFrom` or `yearRangeTo` you will need to make the following change: | ||
|
||
``` | ||
birthday: { type: CalendarDay, yearRangeFrom: 1900, yearRangeTo: 2100 } | ||
``` | ||
becomes | ||
|
||
``` | ||
birthday: { type: CalendarDay, dateFrom: '1900-01-01', dateTo: '2100-12-31' } | ||
``` |
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,71 @@ | ||
const { multiAdapterRunners, setupServer, graphqlRequest } = require('@keystonejs/test-utils'); | ||
|
||
const { CalendarDay } = require('@keystonejs/fields'); | ||
const cuid = require('cuid'); | ||
|
||
function setupKeystone(adapterName) { | ||
return setupServer({ | ||
adapterName, | ||
name: `ks5-testdb-${cuid()}`, | ||
createLists: keystone => { | ||
keystone.createList('User', { | ||
fields: { | ||
birthday: { type: CalendarDay, dateFrom: '2000-01-01', dateTo: '2020-01-01' }, | ||
}, | ||
}); | ||
}, | ||
}); | ||
} | ||
|
||
multiAdapterRunners().map(({ runner, adapterName }) => | ||
describe(`Adapter: ${adapterName}`, () => { | ||
describe('CalendarDay type', () => { | ||
test( | ||
'Valid date passes validation', | ||
runner(setupKeystone, async ({ keystone }) => { | ||
const { data } = await graphqlRequest({ | ||
keystone, | ||
query: `mutation { createUser(data: { birthday: "2001-01-01" }) { birthday } }`, | ||
}); | ||
|
||
expect(data).toHaveProperty('createUser.birthday', '2001-01-01'); | ||
}) | ||
); | ||
|
||
test( | ||
'date === dateTo passes validation', | ||
runner(setupKeystone, async ({ keystone }) => { | ||
const { data } = await graphqlRequest({ | ||
keystone, | ||
query: `mutation { createUser(data: { birthday: "2020-01-01" }) { birthday } }`, | ||
}); | ||
expect(data).toHaveProperty('createUser.birthday', '2020-01-01'); | ||
}) | ||
); | ||
|
||
test( | ||
'date === dateFrom passes validation', | ||
runner(setupKeystone, async ({ keystone }) => { | ||
const { data } = await graphqlRequest({ | ||
keystone, | ||
query: `mutation { createUser(data: { birthday: "2020-01-01" }) { birthday } }`, | ||
}); | ||
expect(data).toHaveProperty('createUser.birthday', '2020-01-01'); | ||
}) | ||
); | ||
|
||
test( | ||
'Invalid date failsvalidation', | ||
runner(setupKeystone, async ({ keystone }) => { | ||
const { errors } = await graphqlRequest({ | ||
keystone, | ||
query: `mutation { createUser(data: { birthday: "3000-01-01" }) { birthday } }`, | ||
}); | ||
expect(errors).toHaveLength(1); | ||
const error = errors[0]; | ||
expect(error.message).toEqual('You attempted to perform an invalid mutation'); | ||
}) | ||
); | ||
}); | ||
}) | ||
); |
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
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
58 changes: 58 additions & 0 deletions
58
packages/fields/src/types/CalendarDay/Implementation.test.js
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 { CalendarDay } from './Implementation'; | ||
import { MockAdapter, MockFieldAdapter } from '@keystonejs/test-utils'; | ||
|
||
const mockAdapter = new MockAdapter(); | ||
const mocks = { | ||
getListByKey: () => {}, | ||
listKey: '', | ||
listAdapter: mockAdapter.newListAdapter(), | ||
fieldAdapterClass: MockFieldAdapter, | ||
defaultAccess: true, | ||
schemaNames: ['public'], | ||
}; | ||
|
||
describe('CalendarDay#implementation', () => { | ||
it('Instantiates correctly if dateFrom is before dateTo', () => { | ||
expect(() => { | ||
new CalendarDay('date', { dateFrom: '2000-01-01', dateTo: '2001-01-01' }, mocks); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('Instantiates correctly with only dateFrom', () => { | ||
expect(() => { | ||
new CalendarDay('date', { dateFrom: '2000-01-01' }, mocks); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('Instantiates correctly with only dateTo', () => { | ||
expect(() => { | ||
new CalendarDay('date', { dateTo: '2000-01-01' }, mocks); | ||
}).not.toThrow(); | ||
}); | ||
|
||
describe('error handling', () => { | ||
it("throws if 'dateTo' is before 'dateFrom'", () => { | ||
return expect( | ||
() => new CalendarDay('date', { dateTo: '2000-01-01', dateFrom: '2020-01-01', mocks }) | ||
).toThrow(); | ||
}); | ||
|
||
it("throws if 'dateTo' === 'dateFrom'", () => { | ||
return expect( | ||
() => new CalendarDay('date', { dateTo: '2020-01-01', dateFrom: '2020-01-01', mocks }) | ||
).toThrow(); | ||
}); | ||
|
||
it("throws if 'dateTo' is invalid", () => { | ||
return expect( | ||
() => new CalendarDay('date', { dateTo: '2000--1--1', dateFrom: '2020-01-01', mocks }) | ||
).toThrow(); | ||
}); | ||
|
||
it("throws if 'dateFrom' is invalid", () => { | ||
return expect( | ||
() => new CalendarDay('date', { dateFrom: '2000--1--1', dateTo: '2020-01-01', mocks }) | ||
).toThrow(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.