Skip to content

Commit

Permalink
Adding in an additional ISO 8601 date format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey Sciuto committed Aug 15, 2012
1 parent 325006f commit f112688
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package org.eel.kitchen.jsonschema.format;

import com.fasterxml.jackson.databind.JsonNode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.eel.kitchen.jsonschema.util.NodeType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;

/**
* Specialized format validator for date/time checking
Expand All @@ -46,29 +48,55 @@ public class AbstractDateFormatSpecifier
private final String errmsg;

/**
* The {@link DateTimeFormatter} to use
* The {@link DateTimeFormatter}s to use
*/
private final DateTimeFormatter dtf;
private final List<DateTimeFormatter> formats = new ArrayList<DateTimeFormatter>();

/**
* Constructor
* Constructor for just one format.
*
* @param fmt The date format
* @param desc the description of the date format
*/
protected AbstractDateFormatSpecifier(final String fmt, final String desc)
{
super(NodeType.STRING);
dtf = DateTimeFormat.forPattern(fmt);
formats.add(DateTimeFormat.forPattern(fmt));
errmsg = String.format("string is not a valid %s", desc);
}


/**
* Constructor for many formats, sharing a common description
*
* @param fmt The date formats
* @param desc the description of the date formats
*/
protected AbstractDateFormatSpecifier(final List<String> fmts, final String desc)
{
super(NodeType.STRING);
for (String fmt : fmts){
formats.add(DateTimeFormat.forPattern(fmt));
}
errmsg = String.format("string is not a valid %s", desc);
}

@Override
final void checkValue(final List<String> messages, final JsonNode instance)
{
try {
dtf.parseDateTime(instance.textValue());
} catch (IllegalArgumentException ignored) {
boolean hadSuccess = false;

// If any of the supplied formats match, allow the data through.
for (DateTimeFormatter dtf : formats){
try {
dtf.parseDateTime(instance.textValue());
hadSuccess = true;
break;
} catch (IllegalArgumentException ignored) {
// Ignore.
}
}

if (!hadSuccess){
messages.add(errmsg);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.eel.kitchen.jsonschema.format;

import java.util.Arrays;

/**
* Validator for the {@code date-time} format specification
*/
Expand All @@ -33,6 +35,8 @@ public static FormatSpecifier getInstance()

private DateTimeFormatSpecifier()
{
super("yyyy-MM-dd'T'HH:mm:ssZ", "ISO 8601 date");
// Note that these are the formatting values for JodaTime's ISODateTimeFormat.dateTime and dateTimeNoMillis
// A nice future improvement would be to make this a default that can be overridden via a properties file.
super(Arrays.asList("yyyy-MM-dd'T'HH:mm:ss.SSSZ","yyyy-MM-dd'T'HH:mm:ssZ"), "ISO 8601 date");
}
}
6 changes: 5 additions & 1 deletion src/test/resources/format/datetime.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
{
"data": "2012-08-07T20:42:32Z",
"valid": true
},
},
{
"data": "2012-08-07T20:42:32.123Z",
"valid": true
},
{
"data": "2012-02-30T00:00:00+0000",
"valid": false
Expand Down

0 comments on commit f112688

Please sign in to comment.