Skip to content

Commit

Permalink
validate: remove custom currency format
Browse files Browse the repository at this point in the history
it wasn't being used and was something I added in preparation for custom `dynenum` keyword #1890
  • Loading branch information
jqnatividad committed Sep 16, 2024
1 parent b1e4625 commit 820182a
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 88 deletions.
29 changes: 26 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ jaq-interpret = "1.5.0"
jaq-parse = "1.0.3"
jemallocator = { version = "0.5", optional = true }
json-objects-to-csv = "0.1.3"
jsonschema = { version = "0.18", features = [
jsonschema = { version = "0.19", features = [
"resolve-file",
"resolve-http",
], default-features = false }
Expand Down
169 changes: 85 additions & 84 deletions src/cmd/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ use rayon::{
iter::{IndexedParallelIterator, ParallelIterator},
prelude::IntoParallelRefIterator,
};
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::{json, value::Number, Map, Value};

use crate::{
config::{Config, Delimiter, DEFAULT_WTR_BUFFER_CAPACITY},
regex_oncelock, util, CliResult,
util, CliResult,
};

// to save on repeated init/allocs
Expand Down Expand Up @@ -239,10 +238,12 @@ fn custom_object_type_factory<'a>(
}

/// Check that a string has some number of digits followed by a dot followed by exactly 2 digits.
fn currency_format_checker(s: &str) -> bool {
let currency_re: &'static Regex = regex_oncelock!("^(0|([1-9]+[0-9]*))(\\.[0-9]{2})$");
currency_re.is_match(s)
}
// fn currency_format_checker(s: &str) -> bool {
// use regex::Regex;
// use crate::regex_oncelock;
// let currency_re: &'static Regex = regex_oncelock!("^(0|([1-9]+[0-9]*))(\\.[0-9]{2})$");
// currency_re.is_match(s)
// }

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
Expand Down Expand Up @@ -504,7 +505,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
Ok(json) => {
// compile JSON Schema
match JSONSchema::options()
.with_format("currency", currency_format_checker)
// .with_format("currency", currency_format_checker)
.with_keyword("ascii-keys", custom_object_type_factory)
.compile(&json)
{
Expand Down Expand Up @@ -1179,83 +1180,83 @@ mod tests_for_schema_validation {
}
}

#[test]
fn test_validate_currency_validator() {
fn schema_currency_json() -> Value {
serde_json::json!({
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "The person's title.",
"minLength": 2
},
"name": {
"type": "string",
"description": "The person's name.",
"minLength": 2
},
"fee": {
"description": "The required fee to see the person.",
"type": "string",
"format": "currency",
"minimum": 18
}
}
})
}

let _ = NULL_TYPE.get_or_init(|| Value::String("null".to_string()));
let csv = "title,name,fee
Professor,Xaviers,60.02123";

let mut rdr = csv::Reader::from_reader(csv.as_bytes());
let headers = rdr.byte_headers().unwrap().clone();
let header_types = get_json_types(&headers, &schema_currency_json()).unwrap();

let record = &rdr.byte_records().next().unwrap().unwrap();

let instance = to_json_instance(&header_types, headers.len(), record).unwrap();

let compiled_schema = JSONSchema::options()
.with_format("currency", currency_format_checker)
.compile(&schema_currency_json())
.expect("Invalid schema");

let result = validate_json_instance(&instance, &compiled_schema);

assert_eq!(
result,
Some(vec![(
"fee".to_owned(),
"\"60.02123\" is not a \"currency\"".to_owned()
)])
);

let csv = "title,name,fee
Professor,Xaviers,60.02";

let mut rdr = csv::Reader::from_reader(csv.as_bytes());
let headers = rdr.byte_headers().unwrap().clone();
let header_types = get_json_types(&headers, &schema_currency_json()).unwrap();

let record = &rdr.byte_records().next().unwrap().unwrap();

let instance = to_json_instance(&header_types, headers.len(), record).unwrap();

let compiled_schema = JSONSchema::options()
.with_format("currency", currency_format_checker)
.compile(&schema_currency_json())
.expect("Invalid schema");

let result = validate_json_instance(&instance, &compiled_schema);

// no validation error for currency format
assert_eq!(result, None);
}
// #[test]
// fn test_validate_currency_validator() {
// fn schema_currency_json() -> Value {
// serde_json::json!({
// "$id": "https://example.com/person.schema.json",
// "$schema": "https://json-schema.org/draft/2020-12/schema",
// "title": "Person",
// "type": "object",
// "properties": {
// "title": {
// "type": "string",
// "description": "The person's title.",
// "minLength": 2
// },
// "name": {
// "type": "string",
// "description": "The person's name.",
// "minLength": 2
// },
// "fee": {
// "description": "The required fee to see the person.",
// "type": "string",
// "format": "currency",
// "minimum": 18
// }
// }
// })
// }

// let _ = NULL_TYPE.get_or_init(|| Value::String("null".to_string()));
// let csv = "title,name,fee
// Professor,Xaviers,60.02123";

// let mut rdr = csv::Reader::from_reader(csv.as_bytes());
// let headers = rdr.byte_headers().unwrap().clone();
// let header_types = get_json_types(&headers, &schema_currency_json()).unwrap();

// let record = &rdr.byte_records().next().unwrap().unwrap();

// let instance = to_json_instance(&header_types, headers.len(), record).unwrap();

// let compiled_schema = JSONSchema::options()
// .with_format("currency", currency_format_checker)
// .compile(&schema_currency_json())
// .expect("Invalid schema");

// let result = validate_json_instance(&instance, &compiled_schema);

// assert_eq!(
// result,
// Some(vec![(
// "fee".to_owned(),
// "\"60.02123\" is not a \"currency\"".to_owned()
// )])
// );

// let csv = "title,name,fee
// Professor,Xaviers,60.02";

// let mut rdr = csv::Reader::from_reader(csv.as_bytes());
// let headers = rdr.byte_headers().unwrap().clone();
// let header_types = get_json_types(&headers, &schema_currency_json()).unwrap();

// let record = &rdr.byte_records().next().unwrap().unwrap();

// let instance = to_json_instance(&header_types, headers.len(), record).unwrap();

// let compiled_schema = JSONSchema::options()
// .with_format("currency", currency_format_checker)
// .compile(&schema_currency_json())
// .expect("Invalid schema");

// let result = validate_json_instance(&instance, &compiled_schema);

// // no validation error for currency format
// assert_eq!(result, None);
// }

fn load_json(uri: &str) -> Result<String, String> {
let json_string = match uri {
Expand Down

0 comments on commit 820182a

Please sign in to comment.