-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for validating output schemas using jsonschema
Signed-off-by: Yoriyasu Yano <[email protected]>
- Loading branch information
1 parent
6789e47
commit 26b8d75
Showing
11 changed files
with
734 additions
and
10 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@fensak-io/senc-types", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": "Fensak, LLC <[email protected]> (https://fensak.io)", | ||
"license": "MPL-2.0", | ||
"description": "Core type definitions for senc.", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ mod logger; | |
mod module_loader; | ||
mod ops; | ||
mod threadpool; | ||
mod validator; | ||
|
||
use std::fs; | ||
use std::path; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Fensak, LLC. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
use std::fs; | ||
use std::io; | ||
use std::path; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use jsonschema::{Draft, JSONSchema}; | ||
|
||
pub trait DataSchema { | ||
fn validate(&self, data: &serde_json::Value) -> Result<()>; | ||
} | ||
|
||
pub struct DataJSONSchema { | ||
schema: JSONSchema, | ||
} | ||
|
||
impl DataSchema for DataJSONSchema { | ||
fn validate(&self, data: &serde_json::Value) -> Result<()> { | ||
match self.schema.validate(data) { | ||
Err(errs) => { | ||
let mut err_strs = Vec::new(); | ||
for err in errs { | ||
let instance_path_str = err.instance_path.to_string(); | ||
let err_str = if instance_path_str == "" { | ||
format!("[.] {}", err).to_string() | ||
} else { | ||
format!("[{}] {}\n", instance_path_str, err).to_string() | ||
}; | ||
err_strs.push(err_str); | ||
} | ||
Err(anyhow!(err_strs.join("\n"))) | ||
} | ||
Ok(result) => Ok(result), | ||
} | ||
} | ||
} | ||
|
||
pub fn new_from_path(schema_path: &path::Path) -> Result<impl DataSchema> { | ||
let schema_file = fs::File::open(schema_path)?; | ||
let schema_reader = io::BufReader::new(schema_file); | ||
let raw_schema: serde_json::Value = serde_json::from_reader(schema_reader)?; | ||
|
||
let maybe_jsonschema: Result<JSONSchema, _> = JSONSchema::options() | ||
.with_draft(Draft::Draft202012) | ||
.compile(&raw_schema); | ||
match maybe_jsonschema { | ||
Ok(jsonschema) => { | ||
return Ok(DataJSONSchema { schema: jsonschema }); | ||
} | ||
Err(err) => { | ||
return Err(anyhow!( | ||
"Could not load schema {}: {}", | ||
schema_path.to_string_lossy(), | ||
err | ||
)); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Fensak, LLC. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
export function main() { | ||
return new senc.OutData({ | ||
schema_path: "schema.json", | ||
data: { | ||
productId: 5, | ||
shouldNotHave: true, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) Fensak, LLC. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
export function main() { | ||
return new senc.OutData({ | ||
schema_path: "schema.json", | ||
data: { productId: 5 }, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$id": "https://example.com/product.schema.json", | ||
"title": "Product", | ||
"description": "A product in the catalog", | ||
"type": "object", | ||
"properties": { | ||
"productId": { | ||
"description": "The unique identifier for a product", | ||
"type": "integer" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} |