| 1 | #![cfg(all(target_arch = "wasm32", target_os = "unknown"))] |
| 2 | |
| 3 | use jsonschema::{self, Draft}; |
| 4 | use serde_json::json; |
| 5 | use wasm_bindgen_test::wasm_bindgen_test; |
| 6 | |
| 7 | #[wasm_bindgen_test] |
| 8 | fn validates_simple_object_instances() { |
| 9 | let schema = json!({ |
| 10 | "type": "object", |
| 11 | "properties": { |
| 12 | "name": { "type": "string" }, |
| 13 | "age": { "type": "integer", "minimum": 0 } |
| 14 | }, |
| 15 | "required": ["name"] |
| 16 | }); |
| 17 | |
| 18 | let validator = jsonschema::validator_for(&schema).expect("valid schema"); |
| 19 | |
| 20 | assert!(validator.is_valid(&json!({"name": "Ferris", "age": 7}))); |
| 21 | assert!(!validator.is_valid(&json!({"name": 9, "age": 7}))); |
| 22 | assert!(!validator.is_valid(&json!({"age": 7}))); |
| 23 | } |
| 24 | |
| 25 | #[wasm_bindgen_test] |
| 26 | fn validates_formats_with_options() { |
| 27 | let options = jsonschema::options() |
| 28 | .with_draft(Draft::Draft202012) |
| 29 | .should_validate_formats(true); |
| 30 | let validator = options |
| 31 | .build(&json!({"type": "string", "format": "email"})) |
| 32 | .expect("schema builds"); |
| 33 | |
| 34 | assert!(validator.is_valid(&json!("[email protected]"))); |
| 35 | assert!(!validator.is_valid(&json!("definitely not an email"))); |
| 36 | } |
| 37 | |
| 38 | #[wasm_bindgen_test] |
| 39 | fn validates_uuid_format() { |
| 40 | let options = jsonschema::options() |
| 41 | .with_draft(Draft::Draft202012) |
| 42 | .should_validate_formats(true); |
| 43 | let validator = options |
| 44 | .build(&json!({"type": "string", "format": "uuid"})) |
| 45 | .expect("schema builds"); |
| 46 | |
| 47 | assert!(validator.is_valid(&json!("67e55044-10b1-426f-9247-bb680e5fe0c8"))); |
| 48 | assert!(!validator.is_valid(&json!("67e5504410b1426f9247bb680e5fe0c8"))); // missing hyphens |
| 49 | } |