Skip to content

Commit

Permalink
Merge pull request #160 from jrave/time-format-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanpenman authored Jul 19, 2022
2 parents a6a4fbb + dee2fa6 commit 25dcdb1
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ doc/html
.idea
cmake-build-*
CMakeFiles/
.vs
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ if(valijson_BUILD_TESTS)
set_target_properties(test_suite PROPERTIES COMPILE_FLAGS " -pedantic -Werror -Wshadow -Wunused")
endif()

if (MSVC)
target_compile_options(test_suite PRIVATE "/bigobj")
endif()

# Definition for using picojson
set_target_properties(test_suite PROPERTIES COMPILE_DEFINITIONS "PICOJSON_USE_INT64")

Expand Down
27 changes: 27 additions & 0 deletions include/valijson/constraints/concrete_constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,33 @@ class EnumConstraint: public BasicConstraint<EnumConstraint>
EnumValues m_enumValues;
};

/**
* @brief Represent a 'format' constraint
*
* A format constraint restricts the content of string values, as defined by a set of commonly used formats.
*
* As this is an optional feature in JSON Schema, unrecognised formats will be treated as valid for any string value.
*/
class FormatConstraint: public BasicConstraint<FormatConstraint>
{
public:
FormatConstraint()
: m_format() { }

const std::string & getFormat() const
{
return m_format;
}

void setFormat(const std::string & format)
{
m_format = format;
}

private:
std::string m_format;
};

/**
* @brief Represents non-singular 'items' and 'additionalItems' constraints
*
Expand Down
3 changes: 3 additions & 0 deletions include/valijson/constraints/constraint_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ConstConstraint;
class ContainsConstraint;
class DependenciesConstraint;
class EnumConstraint;
class FormatConstraint;
class LinearItemsConstraint;
class MaxItemsConstraint;
class MaximumConstraint;
Expand Down Expand Up @@ -46,6 +47,7 @@ class ConstraintVisitor
typedef constraints::ContainsConstraint ContainsConstraint;
typedef constraints::DependenciesConstraint DependenciesConstraint;
typedef constraints::EnumConstraint EnumConstraint;
typedef constraints::FormatConstraint FormatConstraint;
typedef constraints::LinearItemsConstraint LinearItemsConstraint;
typedef constraints::MaximumConstraint MaximumConstraint;
typedef constraints::MaxItemsConstraint MaxItemsConstraint;
Expand Down Expand Up @@ -77,6 +79,7 @@ class ConstraintVisitor
virtual bool visit(const ContainsConstraint &) = 0;
virtual bool visit(const DependenciesConstraint &) = 0;
virtual bool visit(const EnumConstraint &) = 0;
virtual bool visit(const FormatConstraint &) = 0;
virtual bool visit(const LinearItemsConstraint &) = 0;
virtual bool visit(const MaximumConstraint &) = 0;
virtual bool visit(const MaxItemsConstraint &) = 0;
Expand Down
27 changes: 27 additions & 0 deletions include/valijson/schema_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,10 @@ class SchemaParser
rootSchema.addConstraintToSubschema(makeEnumConstraint(itr->second), &subschema);
}

if ((itr = object.find("format")) != object.end()) {
rootSchema.addConstraintToSubschema(makeFormatConstraint(itr->second), &subschema);
}

{
const typename AdapterType::Object::const_iterator itemsItr =
object.find("items");
Expand Down Expand Up @@ -1421,6 +1425,29 @@ class SchemaParser
return constraint;
}

/**
* @brief Make a new FormatConstraint object
*
* @param node JSON node containing the configuration for this constraint
*
* @return pointer to a new FormatConstraint that belongs to the caller
*/
template<typename AdapterType>
constraints::FormatConstraint makeFormatConstraint(
const AdapterType &node)
{
if (node.isString()) {
const std::string value = node.asString();
if (!value.empty()) {
constraints::FormatConstraint constraint;
constraint.setFormat(value);
return constraint;
}
}

throwRuntimeError("Expected a string value for 'format' constraint.");
}

/**
* @brief Make a new ItemsConstraint object.
*
Expand Down
99 changes: 99 additions & 0 deletions include/valijson/validation_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,64 @@ class ValidationVisitor: public constraints::ConstraintVisitor
return numValidated > 0;
}

/**
* @brief Validate current node against a FormatConstraint
*
* @param constraint Constraint that the target must validate against
*
* @return \c true if validation succeeds; \c false otherwise
*/
bool visit(const FormatConstraint &constraint) override
{
const std::string s = m_target.asString();
const std::string format = constraint.getFormat();
if (format == "date") {
// Matches dates like: 2022-07-18
std::regex date_regex("^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
std::smatch matches;
if (std::regex_match(s, matches, date_regex)) {
const auto month = std::stoi(matches[2].str());
const auto day = std::stoi(matches[3].str());
return validate_date_range(month, day);
} else {
if (m_results) {
m_results->pushError(m_context,
"String should be a valid date");
}
return false;
}
} else if (format == "time") {
// Matches times like: 16:52:45Z, 16:52:45+02:00
std::regex time_regex("^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$");
if (std::regex_match(s, time_regex)) {
return true;
} else {
if (m_results) {
m_results->pushError(m_context,
"String should be a valid time");
}
return false;
}
} else if (format == "date-time") {
// Matches data times like: 2022-07-18T16:52:45Z, 2022-07-18T16:52:45+02:00
std::regex datetime_regex("^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$");
std::smatch matches;
if (std::regex_match(s, matches, datetime_regex)) {
const auto month = std::stoi(matches[2].str());
const auto day = std::stoi(matches[3].str());
return validate_date_range(month, day);
} else {
if (m_results) {
m_results->pushError(m_context,
"String should be a valid date-time");
}
return false;
}
}

return true;
}

/**
* @brief Validate a value against a LinearItemsConstraint
*
Expand Down Expand Up @@ -1769,6 +1827,47 @@ class ValidationVisitor: public constraints::ConstraintVisitor
return constraint.accept(visitor);
}

/**
* @brief Helper function to validate if day is valid for given month
*
* @param month Month, 1-12
* @param day Day, 1-31
*
* @return true if day is valid for given month, false otherwise.
*/
bool validate_date_range(int month, int day)
{
if (month == 2) {
if (day < 0 || day > 29) {
if (m_results) {
m_results->pushError(m_context,
"String should be a valid date-time");
}
return false;
}
} else {
int limit = 31;
if (month <= 7) {
if (month % 2 == 0) {
limit = 30;
}
} else {
if (month % 2 != 0) {
limit = 30;
}
}
if (day < 0 || day > limit) {
if (m_results) {
m_results->pushError(m_context,
"String should be a valid date-time");
}
return false;
}

}
return true;
}

/// The JSON value being validated
AdapterType m_target;

Expand Down
18 changes: 18 additions & 0 deletions tests/test_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#include <valijson/adapters/jsoncpp_adapter.hpp>
#include <valijson/adapters/rapidjson_adapter.hpp>
#include <valijson/adapters/picojson_adapter.hpp>
#include <valijson/adapters/nlohmann_json_adapter.hpp>
#include <valijson/utils/json11_utils.hpp>
#include <valijson/utils/jsoncpp_utils.hpp>
#include <valijson/utils/picojson_utils.hpp>
#include <valijson/utils/rapidjson_utils.hpp>
#include <valijson/utils/nlohmann_json_utils.hpp>
#include <valijson/schema.hpp>
#include <valijson/schema_parser.hpp>
#include <valijson/validation_results.hpp>
Expand Down Expand Up @@ -176,6 +178,7 @@ class TestValidator : public ::testing::TestWithParam<const char *>
processTestFile<valijson::adapters::JsonCppAdapter>(testFile, version);
processTestFile<valijson::adapters::RapidJsonAdapter>(testFile, version);
processTestFile<valijson::adapters::PicoJsonAdapter>(testFile, version);
processTestFile<valijson::adapters::NlohmannJsonAdapter>(testFile, version);

#ifdef VALIJSON_BUILD_POCO_ADAPTER
processTestFile<valijson::adapters::PocoJsonAdapter>(testFile, version);
Expand Down Expand Up @@ -605,3 +608,18 @@ TEST_F(TestValidator, Draft7_UniqueItems)
{
processDraft7TestFile(TEST_SUITE_DIR "draft7/uniqueItems.json");
}

TEST_F(TestValidator, Draft7_OptionalFormatDate)
{
processDraft7TestFile(TEST_SUITE_DIR "draft7/optional/format/date.json");
}

TEST_F(TestValidator, Draft7_OptionalFormatTime)
{
processDraft7TestFile(TEST_SUITE_DIR "draft7/optional/format/time.json");
}

TEST_F(TestValidator, Draft7_OptionalFormatDateTime)
{
processDraft7TestFile(TEST_SUITE_DIR "draft7/optional/format/date-time.json");
}

0 comments on commit 25dcdb1

Please sign in to comment.