-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from dhis2/add-date-validation-util
feat(validation): add and expose validateDateString utility
- Loading branch information
Showing
9 changed files
with
202 additions
and
24 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './calendars' | ||
export * from './numberingSystems' | ||
export { calendars } from './calendars' | ||
export { numberingSystems } from './numberingSystems' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './hooks' | ||
export * as constants from './constants' | ||
export * from './period-calculation' | ||
export { default as getNowInCalendar } from './utils/getNowInCalendar' | ||
export { getNowInCalendar, validateDateString } from './utils' |
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 @@ | ||
export const dateStringRegExp = /^(\d{4})([-./])(\d{2})(\2)(\d{2})$/ |
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,16 @@ | ||
import { dateStringRegExp } from './date-string-regexp' | ||
|
||
export function extractDatePartsFromDateString(dateString: string) { | ||
const parts = dateString.match(dateStringRegExp) | ||
|
||
if (!parts) { | ||
throw new Error(`Date string is invalid, received "${dateString}"`) | ||
} | ||
|
||
const [, yearStr, , monthStr, , dayStr] = parts | ||
const year = parseInt(yearStr, 10) | ||
const month = parseInt(monthStr, 10) | ||
const day = parseInt(dayStr, 10) | ||
|
||
return { year, month, day } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { extractDatePartsFromDateString } from './extract-date-parts-from-date-string' | ||
export { default as fromAnyDate } from './from-any-date' | ||
export { default as getNowInCalendar } from './getNowInCalendar' | ||
export * from './helpers' | ||
export { default as localisationHelpers } from './localisationHelpers' | ||
export { validateDateString } from './validate-date-string' |
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,146 @@ | ||
import { Temporal } from '@js-temporal/polyfill' | ||
import { validateDateString } from './validate-date-string' | ||
|
||
describe('validateDateString (gregory)', () => { | ||
it('should return undefined for a date with dashes as delimiter', () => { | ||
expect(validateDateString('2024-02-02')).toBe(undefined) | ||
}) | ||
|
||
it('should return undefined for a date with dashes as slashes', () => { | ||
expect(validateDateString('2024/02/02')).toBe(undefined) | ||
}) | ||
|
||
it('should return undefined for a date with dashes as dots', () => { | ||
expect(validateDateString('2024.02.02')).toBe(undefined) | ||
}) | ||
|
||
it('should return an error message for a date with mixed delimiters', () => { | ||
expect(validateDateString('2024/02.02')).toBe( | ||
'Date string is invalid, received "2024/02.02"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing year digits', () => { | ||
expect(validateDateString('200.02.02')).toBe( | ||
'Date string is invalid, received "200.02.02"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing month digits', () => { | ||
expect(validateDateString('2000.2.02')).toBe( | ||
'Date string is invalid, received "2000.2.02"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing day digits', () => { | ||
expect(validateDateString('2000.02.2')).toBe( | ||
'Date string is invalid, received "2000.02.2"' | ||
) | ||
}) | ||
|
||
it('should return an error message when the value is out of range', () => { | ||
expect(validateDateString('2025-12-32')).toBe( | ||
'value out of range: 1 <= 32 <= 31' | ||
) | ||
}) | ||
}) | ||
|
||
describe('validateDateString (ethiopic)', () => { | ||
it('should return undefined for a date with dashes as delimiter', () => { | ||
expect(validateDateString('2015-13-06', { calendar: 'ethiopic' })).toBe( | ||
undefined | ||
) | ||
}) | ||
|
||
it('should return undefined for a date with dashes as slashes', () => { | ||
expect(validateDateString('2015/13/06', { calendar: 'ethiopic' })).toBe( | ||
undefined | ||
) | ||
}) | ||
|
||
it('should return undefined for a date with dashes as dots', () => { | ||
expect(validateDateString('2015.13.06', { calendar: 'ethiopic' })).toBe( | ||
undefined | ||
) | ||
}) | ||
|
||
it('should return an error message for a date with mixed delimiters', () => { | ||
expect(validateDateString('2015.13/06', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "2015.13/06"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing year digits', () => { | ||
expect(validateDateString('201.13/06', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "201.13/06"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing month digits', () => { | ||
expect(validateDateString('201.1/06', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "201.1/06"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing day digits', () => { | ||
expect(validateDateString('2015.13/6', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "2015.13/6"' | ||
) | ||
}) | ||
|
||
it('should return an error message when the value is out of range', () => { | ||
expect(validateDateString('2015-14-01', { calendar: 'ethiopic' })).toBe( | ||
'value out of range: 1 <= 14 <= 13' | ||
) | ||
}) | ||
}) | ||
|
||
describe('validateDateString (nepali)', () => { | ||
it('should return undefined for a date with dashes as delimiter', () => { | ||
expect(validateDateString('2080-10-29', { calendar: 'ethiopic' })).toBe( | ||
undefined | ||
) | ||
}) | ||
|
||
it('should return undefined for a date with dashes as slashes', () => { | ||
expect(validateDateString('2080/10/29', { calendar: 'ethiopic' })).toBe( | ||
undefined | ||
) | ||
}) | ||
|
||
it('should return undefined for a date with dashes as dots', () => { | ||
expect(validateDateString('2080.10.29', { calendar: 'ethiopic' })).toBe( | ||
undefined | ||
) | ||
}) | ||
|
||
it('should return an error message for a date with mixed delimiters', () => { | ||
expect(validateDateString('2080.10/29', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "2080.10/29"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing year digits', () => { | ||
expect(validateDateString('280.10.29', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "280.10.29"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing month digits', () => { | ||
expect(validateDateString('2080.1.29', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "2080.1.29"' | ||
) | ||
}) | ||
|
||
it('should return an error message for a date missing day digits', () => { | ||
expect(validateDateString('2080.10.9', { calendar: 'ethiopic' })).toBe( | ||
'Date string is invalid, received "2080.10.9"' | ||
) | ||
}) | ||
|
||
it('should return an error message when the value is out of range', () => { | ||
expect(validateDateString('2080.14.30', { calendar: 'ethiopic' })).toBe( | ||
'value out of range: 1 <= 14 <= 13' | ||
) | ||
}) | ||
}) |
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,29 @@ | ||
import { Temporal } from '@js-temporal/polyfill' | ||
import { dhis2CalendarsMap } from '../constants/dhis2CalendarsMap' | ||
import type { SupportedCalendar } from '../types' | ||
import { extractDatePartsFromDateString } from './extract-date-parts-from-date-string' | ||
import { getCustomCalendarIfExists } from './helpers' | ||
|
||
export function validateDateString( | ||
dateString: string, | ||
{ calendar = 'gregory' }: { calendar?: SupportedCalendar } = {} | ||
): undefined | string { | ||
const resolvedCalendar = getCustomCalendarIfExists( | ||
dhis2CalendarsMap[calendar] ?? calendar | ||
) as SupportedCalendar | ||
|
||
try { | ||
// will throw if the format of the date is incorrect | ||
const { year, month, day } = extractDatePartsFromDateString(dateString) | ||
|
||
// will throw if the year, month or day is out of range | ||
Temporal.PlainDate.from( | ||
{ year, month, day, calendar: resolvedCalendar }, | ||
{ overflow: 'reject' } | ||
) | ||
} catch (e) { | ||
return (e as Error).message | ||
} | ||
|
||
return undefined | ||
} |