-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds ISO 8601 Full Date rule (#1702)
* feat: Adds ISO 8601 Full Date validation * docs: Removes incorrect statement
- Loading branch information
Showing
5 changed files
with
129 additions
and
0 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
52 changes: 52 additions & 0 deletions
52
src/constrainedStrings/iso8601FullDate/iso8601FullDate.test.ts
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,52 @@ | ||
import * as assert from 'assert' | ||
import { iso8601FullDate, ISO8601FullDate, InvalidISO8601FullDateError } from '../../rulr' | ||
|
||
test('ISO 8601 Full Date should not allow invalid string input', () => { | ||
const input = 0 | ||
assert.throws(() => iso8601FullDate(input), InvalidISO8601FullDateError) | ||
}) | ||
|
||
test('ISO 8601 Full Date should allow valid ISO 8601 Full Date', () => { | ||
const input = '2020-01-01' | ||
const output: ISO8601FullDate = iso8601FullDate(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
test('ISO 8601 Full Date should not allow invalid ISO 8601 Full Date', () => { | ||
const input = '2020-01-01T00:00Z' | ||
assert.throws(() => iso8601FullDate(input), InvalidISO8601FullDateError) | ||
}) | ||
|
||
test('ISO 8601 Full Date should not allow basic format ISO 8601 Full Date', () => { | ||
const input = '20090131' | ||
assert.throws(() => iso8601FullDate(input), InvalidISO8601FullDateError) | ||
}) | ||
|
||
test('ISO 8601 Full Date should allow valid extended format ISO 8601 Full Date', () => { | ||
const input = '2009-01-31' | ||
const output: ISO8601FullDate = iso8601FullDate(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
// http://stackoverflow.com/questions/12756159 | ||
test('ISO 8601 Full Date should allow old ISO 8601 Full Date', () => { | ||
const input = '0785-10-10' | ||
const output: ISO8601FullDate = iso8601FullDate(input) | ||
assert.strictEqual(output, input) | ||
}) | ||
|
||
// http://stackoverflow.com/questions/12756159 | ||
test('ISO 8601 Full Date should not allow invalid month and date', () => { | ||
const input = '2013-99-99' | ||
assert.throws(() => iso8601FullDate(input), InvalidISO8601FullDateError) | ||
}) | ||
|
||
test('ISO 8601 Full Date should not allow year only', () => { | ||
const input = '2017' | ||
assert.throws(() => iso8601FullDate(input), InvalidISO8601FullDateError) | ||
}) | ||
|
||
test('ISO 8601 Full Date should not allow year and month only', () => { | ||
const input = '2017-01' | ||
assert.throws(() => iso8601FullDate(input), InvalidISO8601FullDateError) | ||
}) |
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,30 @@ | ||
import { BaseError } from 'make-error' | ||
import { isString } from '../../valueRules/string/string' | ||
import { Constrained } from '../../core' | ||
|
||
export class InvalidISO8601FullDateError extends BaseError { | ||
constructor() { | ||
super('expected ISO 8601 Full Date') | ||
} | ||
} | ||
|
||
const iso8601FullDateRegex = /^\d\d\d\d-\d\d-\d\d$/i | ||
|
||
export const iso8601FullDateSymbol = Symbol() | ||
|
||
export type ISO8601FullDate = Constrained<typeof iso8601FullDateSymbol, string> | ||
|
||
export function isISO8601FullDate(input: unknown): input is ISO8601FullDate { | ||
if (isString(input) && iso8601FullDateRegex.test(input)) { | ||
const date = new Date(input); | ||
return !isNaN(date.getTime()); | ||
} | ||
return false; | ||
} | ||
|
||
export function iso8601FullDate(input: unknown) { | ||
if (isISO8601FullDate(input)) { | ||
return input | ||
} | ||
throw new InvalidISO8601FullDateError() | ||
} |
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,40 @@ | ||
# iso8601FullDate | ||
|
||
[Back to root readme.md](../../../readme.md) | ||
|
||
This function uses the `rulr.isISO8601FullDate` guard to check the input is a valid ISO 8601 Full Date as shown in the example below. It should only throw `rulr.InvalidISO8601FullDateError`. | ||
|
||
```ts | ||
import * as rulr from 'rulr' | ||
|
||
const constrainToExample = rulr.object({ | ||
required: { | ||
example: rulr.iso8601FullDate, | ||
}, | ||
}) | ||
|
||
type Example = rulr.Static<typeof constrainToExample> | ||
// { | ||
// example: rulr.ISO8601FullDate | ||
// } | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: '2020-01-01', | ||
}) | ||
|
||
// Invalid: Not a valid ISO 8601 Full Date | ||
const example2: Example = constrainToExample({ | ||
example: '2020-01-01T00:00Z', | ||
}) | ||
|
||
// Invalid: Not a valid ISO 8601 Full Date | ||
const example2: Example = constrainToExample({ | ||
example: '2020-01', | ||
}) | ||
|
||
// Invalid: Not a string | ||
const example3: Example = constrainToExample({ | ||
example: 1, | ||
}) | ||
``` |
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