Skip to content

Commit

Permalink
Merge pull request #26 from waitingcheung/dateformat
Browse files Browse the repository at this point in the history
Throw an error for invalid date format
  • Loading branch information
MarcusLongmuir committed Jan 14, 2017
2 parents 37b911f + 2bbb9f8 commit c64dd0d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -3,6 +3,10 @@ FieldVal-JS

The FieldVal-JS library allows you to easily validate data and provide readable and structured error reports.

[![MinoHubs](https://www.minohubs.com/badge/fieldval/support.svg)](https://www.minohubs.com/hub/fieldval)

[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/FieldVal/fieldval-js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Documentation and Examples
=============

Expand All @@ -17,4 +21,4 @@ This project uses [gulp.js](http://gulpjs.com/) to build and [mocha](http://visi
npm install
gulp js
mocha test/test
```
```
4 changes: 4 additions & 0 deletions src/DateVal.js
Expand Up @@ -72,6 +72,10 @@ var DateVal = (function(){
date_with_format_array: function(date, format_array){
//Takes a Javascript Date object

if (!Array.isArray(format_array)) {
throw new Error('Not a valid date format');
}

var date_string = "";

for(var i = 0; i < format_array.length; i++){
Expand Down
13 changes: 13 additions & 0 deletions test/DateVal_test.js
Expand Up @@ -142,6 +142,19 @@ describe('DateVal', function() {

assert.equal(null, my_validator.end());
})

it('should throw an exception for invalid date format', function() {

var my_validator = new FieldVal({
"my_format": "yyy-MM-dd hh:mm:ss"
})

var format_array = my_validator.get("my_format", BasicVal.string(true), BasicVal.date_format());

var test_date = new Date(Date.UTC(2014, 08, 10, 16, 05, 38));//'Wed Sep 10 2014 16:05:38 GMT+0100 (BST)');

assert.throws(function() { DateVal.date_with_format_array(test_date, format_array); }, Error);
})
})

describe('pad_to_valid()', function() {
Expand Down

0 comments on commit c64dd0d

Please sign in to comment.