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

Extra format support for ISO currency codes #181

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,67 @@
/*
* 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.extra;

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 java.util.Currency;
import java.util.UUID;

/**
* Format specifier for a proposed {@code uuid} attribute
*
* @see UUID#fromString(String)
*/
public final class CurrencyFormatAttribute
extends AbstractFormatAttribute
{
private static final FormatAttribute instance = new CurrencyFormatAttribute();

private CurrencyFormatAttribute()
{
super("currency", NodeType.STRING);
}

public static FormatAttribute getInstance()
{
return instance;
}

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

try {
Currency.getInstance(input);
} catch (IllegalArgumentException ignored) {
report.error(newMsg(data, bundle, "err.format.currency.invalid")
.putArgument("value", input));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.fge.jsonschema.core.util.DictionaryBuilder;
import com.github.fge.jsonschema.format.FormatAttribute;
import com.github.fge.jsonschema.format.extra.Base64FormatAttribute;
import com.github.fge.jsonschema.format.extra.CurrencyFormatAttribute;
import com.github.fge.jsonschema.format.extra.JsonPointerFormatAttribute;
import com.github.fge.jsonschema.format.extra.MD5FormatAttribute;
import com.github.fge.jsonschema.format.extra.MacAddressFormatAttribute;
Expand Down Expand Up @@ -78,6 +79,10 @@ private ExtraFormatsDictionary()
attribute = UUIDFormatAttribute.getInstance();
builder.addEntry(name, attribute);

name = "currency";
attribute = CurrencyFormatAttribute.getInstance();
builder.addEntry(name, attribute);

DICTIONARY = builder.freeze();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ err.format.jsonpointer.invalid = input string "%s" is not a valid JSON Pointer
err.format.macAddr.invalid = input string "%s" is not a valid MAC address
err.format.uriTemplate.invalid = input string "%s" is not a valid URI template
err.format.UUID.invalid = input string "%s" is not a valid UUID
err.format.currency.invalid = input string "%s" is not a supported ISO 4217 currency code

#
# Other messages
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.extra;

import java.io.IOException;

public final class CurrencyFormatAttributeTest
extends ExtraFormatAttributeTest
{
public CurrencyFormatAttributeTest()
throws IOException
{
super("currency");
}
}
19 changes: 19 additions & 0 deletions src/test/resources/format/extra/currency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[
{
"data": "USD",
"valid": true
},
{
"data": "CAD",
"valid": true
},
{
"data": "ATF",
"valid": false,
"message": "err.format.currency.invalid",
"msgData": {
"value": "ATF"
},
"msgParams": [ "value" ]
}
]