Function regex_from_value

Source
pub fn regex_from_value(
    json: &Value,
    whitespace_pattern: Option<&str>,
) -> Result<String>
Expand description

Generates a regular expression string from serde_json::Value type of JSON schema.

ยงExample

use serde_json::Value;
use outlines_core::prelude::*;

    // Define a JSON schema
    let schema = r#"{
        "type": "object",
        "properties": {
            "name": { "type": "string" },
            "age": { "type": "integer" }
        },
        "required": ["name", "age"]
    }"#;

    // If schema's `Value` was already parsed
    let schema_value: Value = serde_json::from_str(schema)?;

    // It's possible to generate a regex from schema value
    let regex = json_schema::regex_from_value(&schema_value, None)?;
    println!("Generated regex: {}", regex);

    // Custom whitespace pattern could be passed as well
    let whitespace_pattern = Some(r#"[\n ]*"#);
    let regex = json_schema::regex_from_value(&schema_value, whitespace_pattern)?;
    println!("Generated regex with custom whitespace pattern: {}", regex);

}