From 2e10faf5936995fac82c991b959f99d08a7366b9 Mon Sep 17 00:00:00 2001 From: BrianMichell Date: Wed, 31 Jul 2024 14:09:10 +0000 Subject: [PATCH 1/2] Updated to use built in datetime validation --- mdio/dataset_schema.h | 9 ++++++++- mdio/dataset_validator.h | 35 ++-------------------------------- mdio/dataset_validator_test.cc | 8 -------- 3 files changed, 10 insertions(+), 42 deletions(-) diff --git a/mdio/dataset_schema.h b/mdio/dataset_schema.h index 21eeb17..f15f743 100644 --- a/mdio/dataset_schema.h +++ b/mdio/dataset_schema.h @@ -500,6 +500,12 @@ buckets. "title": "Apiversion", "type": "string" }, + "createdOn": { + "description": "The timestamp indicating when the dataset was first created, including timezone information. Expressed in ISO 8601 format.", + "format": "date-time", + "title": "Createdon", + "type": "string" + }, "attributes": { "anyOf": [ { @@ -516,7 +522,8 @@ buckets. }, "required": [ "name", - "apiVersion" + "apiVersion", + "createdOn" ], "title": "DatasetMetadata", "type": "object" diff --git a/mdio/dataset_validator.h b/mdio/dataset_validator.h index 30507c3..10f566a 100644 --- a/mdio/dataset_validator.h +++ b/mdio/dataset_validator.h @@ -16,7 +16,6 @@ #define MDIO_DATASET_VALIDATOR_H_ #include -#include // NOLINT #include #include @@ -27,17 +26,6 @@ #include // NOLINT // clang-format on -/** - * @brief Checks if a string is a valid ISO8601 datetime - * @param dateTimeStr A string representing a datetime - * @return True if valid, false otherwise - */ -bool isISO8601DateTime(const std::string& dateTimeStr) { - std::regex iso8601Regex( - R"(^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}(-\d{2}:\d{2}|Z)$)"); - return std::regex_match(dateTimeStr, iso8601Regex); -} - /** * @brief Checks if a key exists in a map * Specific for our case of {coordinate: index} mapping @@ -55,26 +43,10 @@ bool contains(const std::unordered_set& set, * InvalidArgumentError if validation fails for any reason */ absl::Status validate_schema(nlohmann::json& spec /*NOLINT*/) { - // This is a hack to fix the date-time format not working as intended with the - // json-schema-validator - - nlohmann::json createdOn = nlohmann::json::object(); - if (spec.contains("metadata")) { - if (!spec["metadata"].contains("createdOn")) { - return absl::Status(absl::StatusCode::kInvalidArgument, - "CreatedOn field not found."); - } - createdOn = spec["metadata"]["createdOn"]; - spec["metadata"].erase("createdOn"); - if (!isISO8601DateTime(createdOn.get())) { - return absl::Status(absl::StatusCode::kInvalidArgument, - "CreatedOn field is not a valid ISO8601 datetime."); - } - } - nlohmann::json targetSchema = nlohmann::json::parse(kDatasetSchema); - nlohmann::json_schema::json_validator validator; + nlohmann::json_schema::json_validator validator( + nullptr, nlohmann::json_schema::default_string_format_check); validator.set_root_schema(targetSchema); try { @@ -85,9 +57,6 @@ absl::Status validate_schema(nlohmann::json& spec /*NOLINT*/) { "Validation failed, here is why: " + std::string(e.what())); } - if (!createdOn.empty()) { - spec["metadata"]["createdOn"] = createdOn; - } return absl::OkStatus(); } diff --git a/mdio/dataset_validator_test.cc b/mdio/dataset_validator_test.cc index 9f88113..363f4ef 100644 --- a/mdio/dataset_validator_test.cc +++ b/mdio/dataset_validator_test.cc @@ -899,14 +899,6 @@ TEST(validate, valid) { TEST(validate, invalid) { EXPECT_TRUE(true); } -TEST(datetime, valid) { - EXPECT_TRUE(isISO8601DateTime("2023-12-12T15:02:06.413469-06:00")); -} - -TEST(datetime, invalid) { - EXPECT_FALSE(isISO8601DateTime("2023-12-12T15:02:06.413469-06:")); -} - // TODO(BrianMichell): Validate that the shapes are all valid // TODO(BrianMichell): Return a list of Variable specs + metadata From 96aef21c7b684967ac1bcc86f5d967e685c23632 Mon Sep 17 00:00:00 2001 From: BrianMichell Date: Wed, 31 Jul 2024 14:09:52 +0000 Subject: [PATCH 2/2] Removed unimplemented test --- mdio/dataset_validator_test.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/mdio/dataset_validator_test.cc b/mdio/dataset_validator_test.cc index 363f4ef..dd4be1c 100644 --- a/mdio/dataset_validator_test.cc +++ b/mdio/dataset_validator_test.cc @@ -897,8 +897,6 @@ TEST(validate, valid) { EXPECT_TRUE(status.ok()) << status; } -TEST(validate, invalid) { EXPECT_TRUE(true); } - // TODO(BrianMichell): Validate that the shapes are all valid // TODO(BrianMichell): Return a list of Variable specs + metadata