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

Fixes/adds new date-time validator #20

Merged
merged 1 commit into from
Mar 18, 2025
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ csv = "1.1"
env_logger = "0.11.7"
jsonschema = "0.16"
log = "0.4"
regex = "1.10"
reqwest = { version = "0.11", features = ["blocking"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use jsonschema::{Draft, JSONSchema};
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};
use log::{debug, error, warn};
use regex::Regex;

// Initialize env_logger to target stderr.
use env_logger;
Expand Down Expand Up @@ -49,6 +50,13 @@ struct StateMessage {
value: Value,
}

// Custom format validator for date-time that accepts both formats with and without timezone
fn validate_datetime(value: &str) -> bool {
// Regex for ISO 8601 date-time with optional timezone
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?$").unwrap();
re.is_match(value)
}

fn emit_state(state: &Option<Value>) {
// The state message is printed to stdout.
let state_value = state.clone().unwrap_or(json!({}));
Expand Down Expand Up @@ -245,6 +253,7 @@ fn persist_messages(
if validate {
let compiled = match JSONSchema::options()
.with_draft(Draft::Draft4)
.with_format("date-time", validate_datetime)
.compile(&schema_message.schema)
{
Ok(schema) => schema,
Expand Down