Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
[modules] add Date refinament which expects ISO 8601 date
Browse files Browse the repository at this point in the history
  • Loading branch information
ArmorDarks committed Dec 17, 2017
1 parent fa391a9 commit 7350d3c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added

- [styles] Added `text-decoration-skip: ink;` to default links style.
- [modules] Added `Date` refinament which expects ISO 8601 date.

### Changed
- [package] Updated dependencies.
Expand Down
2 changes: 2 additions & 0 deletions modules/refinements.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const t = require('tcomb')
const moment = require('moment')

module.exports = {
False: t.refinement(t.Boolean, (b) => b === false, 'False'),
Absoluteurl: t.refinement(t.String, (u) => /^\/\/|:\/\//.test(u), 'Absolute url'),
Imagepath: t.refinement(t.String, (i) => /.(jpg|jpeg|gif|png|svg)$/.test(i), 'Image file'),
Handle: t.refinement(t.String, (i) => /^@((?!(:|\\|\/)).)*$/.test(i), 'Handle'),
Date: t.refinement(t.String, (d) => moment(d, moment.ISO_8601, true).isValid(), 'ISO 8601 Date'),

/**
* Ensures that value does not exceed specified length
Expand Down
12 changes: 12 additions & 0 deletions tests/kotsu/__snapshots__/refinements.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ exports[`Tcomb refinement Absoluteurl should pass absolute url 2`] = `"https://t

exports[`Tcomb refinement Absoluteurl should pass absolute url 3`] = `"//test.com"`;

exports[`Tcomb refinement Date should error on non-ISO 8601 date 1`] = `"[tcomb] Invalid value 1 supplied to ISO 8601 Date"`;

exports[`Tcomb refinement Date should error on non-ISO 8601 date 2`] = `"[tcomb] Invalid value \\"test\\" supplied to ISO 8601 Date"`;

exports[`Tcomb refinement Date should error on non-ISO 8601 date 3`] = `"[tcomb] Invalid value \\"2012\\" supplied to ISO 8601 Date"`;

exports[`Tcomb refinement Date should error on non-ISO 8601 date 4`] = `"[tcomb] Invalid value \\"2012-30-30\\" supplied to ISO 8601 Date"`;

exports[`Tcomb refinement Date should error on non-ISO 8601 date 5`] = `"[tcomb] Invalid value \\"2010-01-01T05:\\" supplied to ISO 8601 Date"`;

exports[`Tcomb refinement Date should pass ISO 8601 date 1`] = `"2010-01-01T05:06:07"`;

exports[`Tcomb refinement EqualKeyAndProp should error on everything except object with equal keys and property value 1`] = `"[tcomb] ivalid value \`235\`<number> supplied to \`Testdata/235/id\` property: expected equal to key value \`235\`<string>"`;
exports[`Tcomb refinement EqualKeyAndProp should error on everything except object with equal keys and property value 2`] = `"[tcomb] ivalid value \`111\`<number> supplied to \`Testdata/235/id\` property: expected equal to key value \`235\`<string>"`;
Expand Down
13 changes: 13 additions & 0 deletions tests/kotsu/refinements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ describe('Tcomb refinement', () => {
})
})

describe('Date', () => {
it('should pass ISO 8601 date', () => {
expect(r.Date('2010-01-01T05:06:07')).toMatchSnapshot()
})
it('should error on non-ISO 8601 date', () => {
expect(() => r.Date(1)).toThrowErrorMatchingSnapshot()
expect(() => r.Date('test')).toThrowErrorMatchingSnapshot()
expect(() => r.Date('2012')).toThrowErrorMatchingSnapshot()
expect(() => r.Date('2012-30-30')).toThrowErrorMatchingSnapshot()
expect(() => r.Date('2010-01-01T05:')).toThrowErrorMatchingSnapshot()
})
})

describe('EqualKeyAndProp', () => {
const EqualKeysAndId = r.EqualKeyAndProp('id')(t.dict(t.String, t.Any, 'Testdata'))

Expand Down

0 comments on commit 7350d3c

Please sign in to comment.