Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Json Schema Validator for date field #256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2014, Francis Galiegue ([email protected])
*
* This software is dual-licensed under:
*
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
* later version;
* - the Apache Software License (ASL) version 2.0.
*
* The text of this file and of both licenses is available at the root of this
* project or, if you have the jar distribution, in directory META-INF/, under
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
*
* Direct link to the sources:
*
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
*/

package com.github.fge.jsonschema.format.common;

import static org.joda.time.DateTimeFieldType.dayOfMonth;
import static org.joda.time.DateTimeFieldType.monthOfYear;
import static org.joda.time.DateTimeFieldType.year;

import com.github.fge.jackson.NodeType;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.format.AbstractFormatAttribute;
import com.github.fge.jsonschema.format.FormatAttribute;
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.google.common.collect.ImmutableList;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;

import java.util.List;

/**
* Validator for the {@code date} format attribute
*/
public final class DateAttribute
extends AbstractFormatAttribute
{
private static final List<String> FORMATS = ImmutableList.of(
"yyyy-MM-dd"
);
private static final DateTimeFormatter FORMATTER;

static {

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder()
.appendFixedDecimal(year(), 4)
.appendLiteral('-')
.appendFixedDecimal(monthOfYear(), 2)
.appendLiteral('-')
.appendFixedDecimal(dayOfMonth(), 2);

FORMATTER = builder.toFormatter();
}

private static final FormatAttribute INSTANCE = new DateAttribute();

public static FormatAttribute getInstance()
{
return INSTANCE;
}

private DateAttribute()
{
super("date", NodeType.STRING);
}

@Override
public void validate(final ProcessingReport report,
final MessageBundle bundle, final FullData data)
throws ProcessingException
{
final String value = data.getInstance().getNode().textValue();

try {
FORMATTER.parseDateTime(value);
} catch (IllegalArgumentException ignored) {
report.error(newMsg(data, bundle, "err.format.invalidDate")
.putArgument("value", value).putArgument("expected", FORMATS));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.fge.jsonschema.core.util.Dictionary;
import com.github.fge.jsonschema.core.util.DictionaryBuilder;
import com.github.fge.jsonschema.format.FormatAttribute;
import com.github.fge.jsonschema.format.common.DateAttribute;
import com.github.fge.jsonschema.format.common.EmailAttribute;
import com.github.fge.jsonschema.format.common.IPv6Attribute;
import com.github.fge.jsonschema.format.common.RFC3339DateTimeAttribute;
Expand Down Expand Up @@ -52,6 +53,10 @@ private CommonFormatAttributesDictionary()
attribute = RFC3339DateTimeAttribute.getInstance();
builder.addEntry(name, attribute);

name = "date";
attribute = DateAttribute.getInstance();
builder.addEntry(name, attribute);

name = "email";
attribute = EmailAttribute.getInstance();
builder.addEntry(name, attribute);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2014, Francis Galiegue ([email protected])
*
* This software is dual-licensed under:
*
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
* later version;
* - the Apache Software License (ASL) version 2.0.
*
* The text of this file and of both licenses is available at the root of this
* project or, if you have the jar distribution, in directory META-INF/, under
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
*
* Direct link to the sources:
*
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
*/

package com.github.fge.jsonschema.format.common;

import java.io.IOException;

public final class DateTest
extends CommonFormatAttributeTest
{
public DateTest()
throws IOException
{
super("date");
}
}
80 changes: 80 additions & 0 deletions src/test/resources/format/common/date.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[
{
"data": "2012-12-02",
"valid": true
},
{
"data": "2001-01-12",
"valid": true
},
{
"data": "2012",
"valid": false,
"message": "err.format.invalidDate",
"msgData": {
"value": "2012",
"expected": [ "yyyy-MM-dd" ]
},
"msgParams": [ "value", "expected" ]
},
{
"data": "2012-01",
"valid": false,
"message": "err.format.invalidDate",
"msgData": {
"value": "2012-01",
"expected": [ "yyyy-MM-dd" ]
},
"msgParams": [ "value", "expected" ]
},
{
"data": "2012-01-32",
"valid": false,
"message": "err.format.invalidDate",
"msgData": {
"value": "2012-01-32",
"expected": [ "yyyy-MM-dd" ]
},
"msgParams": [ "value", "expected" ]
},
{
"data": "2012-09-31",
"valid": false,
"message": "err.format.invalidDate",
"msgData": {
"value": "2012-09-31",
"expected": [ "yyyy-MM-dd" ]
},
"msgParams": [ "value", "expected" ]
},
{
"data": "201202030",
"valid": false,
"message": "err.format.invalidDate",
"msgData": {
"value": "201202030",
"expected": [ "yyyy-MM-dd" ]
},
"msgParams": [ "value", "expected" ]
},
{
"data": "2012-12-02T13:05:00+0100",
"valid": false,
"message": "err.format.invalidDate",
"msgData": {
"value": "2012-12-02T13:05:00+0100",
"expected": [ "yyyy-MM-dd" ]
},
"msgParams": [ "value", "expected" ]
},
{
"data": "a",
"valid": false,
"message": "err.format.invalidDate",
"msgData": {
"value": "a",
"expected": [ "yyyy-MM-dd" ]
},
"msgParams": [ "value", "expected" ]
}
]