File: wasm.rs

package info (click to toggle)
rust-jsonschema 0.37.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,492 kB
  • sloc: makefile: 2
file content (49 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![cfg(all(target_arch = "wasm32", target_os = "unknown"))]

use jsonschema::{self, Draft};
use serde_json::json;
use wasm_bindgen_test::wasm_bindgen_test;

#[wasm_bindgen_test]
fn validates_simple_object_instances() {
    let schema = json!({
        "type": "object",
        "properties": {
            "name": { "type": "string" },
            "age": { "type": "integer", "minimum": 0 }
        },
        "required": ["name"]
    });

    let validator = jsonschema::validator_for(&schema).expect("valid schema");

    assert!(validator.is_valid(&json!({"name": "Ferris", "age": 7})));
    assert!(!validator.is_valid(&json!({"name": 9, "age": 7})));
    assert!(!validator.is_valid(&json!({"age": 7})));
}

#[wasm_bindgen_test]
fn validates_formats_with_options() {
    let options = jsonschema::options()
        .with_draft(Draft::Draft202012)
        .should_validate_formats(true);
    let validator = options
        .build(&json!({"type": "string", "format": "email"}))
        .expect("schema builds");

    assert!(validator.is_valid(&json!("demo@example.com")));
    assert!(!validator.is_valid(&json!("definitely not an email")));
}

#[wasm_bindgen_test]
fn validates_uuid_format() {
    let options = jsonschema::options()
        .with_draft(Draft::Draft202012)
        .should_validate_formats(true);
    let validator = options
        .build(&json!({"type": "string", "format": "uuid"}))
        .expect("schema builds");

    assert!(validator.is_valid(&json!("67e55044-10b1-426f-9247-bb680e5fe0c8")));
    assert!(!validator.is_valid(&json!("67e5504410b1426f9247bb680e5fe0c8"))); // missing hyphens
}