Crate json_mask

source ·
Expand description

githubcrates-iodocs-rs


This library provides mask::JsonMasker which accepts a JSON Schema document.

An example use case is an API where the backend generates the latest response version, but applies the mask to transform the latest response into other API versions to satisfy backwards compatibility.

This is an early build where the input validation is more flexible. Based on real world usage the first stable release will probably restrict to formal schema draft standards and/or allow you to restrict to specific ones.

Examples

  • Use generate a mask with from_str or from_reader and apply it to a document.

    use json_mask::from_str;
    use json_mask::JsonMasker;
    use json_mask::ValidJsonSchema;
    
    let mut document = serde_json::from_str(r#"{ "foo": 1, "bar": 2}"#).unwrap();
    let schema = r#"
    {
      "$schema": "http://json-schema.org/draft-04/schema",
      "title": "Demo Schema",
      "description": "Demo",
      "type": "object",
      "properties": {
        "foo": {
          "type": "integer"
        }
      }
    }
    "#;
    
    let mask = from_str(schema).unwrap();
    let masker = JsonMasker::new(mask);
    masker.mask(&mut document);
    
    assert_eq!(r#"{"foo":1}"#, serde_json::to_string(&document).unwrap())

Structs

Enums

Functions