-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e90419c
commit 7203955
Showing
9 changed files
with
88 additions
and
97 deletions.
There are no files selected for viewing
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,2 +1,5 @@ | ||
#[cfg(feature = "jsonschema")] | ||
pub mod jsonschema; | ||
|
||
#[cfg(feature = "fluent")] | ||
pub mod fluent; |
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 +1,61 @@ | ||
pub mod context; | ||
use std::{ | ||
any::{type_name, TypeId}, | ||
cell::RefCell, | ||
collections::{HashMap, VecDeque}, | ||
}; | ||
|
||
use jsonschema::{ | ||
output::{BasicOutput, ErrorDescription, OutputUnit}, | ||
JSONSchema, | ||
}; | ||
use schemars::gen::{SchemaGenerator, SchemaSettings}; | ||
use serde_json::{Map, Value}; | ||
|
||
thread_local! { | ||
static CONTEXT: RefCell<SchemaContext> = RefCell::new(SchemaContext::new()); | ||
} | ||
|
||
pub(crate) struct SchemaContext { | ||
pub generator: SchemaGenerator, | ||
pub schemas: HashMap<TypeId, JSONSchema>, | ||
} | ||
|
||
impl SchemaContext { | ||
pub fn new() -> Self { | ||
Self { | ||
generator: SchemaSettings::draft07() | ||
.with(|settings| settings.inline_subschemas = true) | ||
.into_generator(), | ||
schemas: HashMap::default(), | ||
} | ||
} | ||
|
||
pub fn validate<T>(value: &Value) -> Result<(), VecDeque<OutputUnit<ErrorDescription>>> | ||
where | ||
T: crate::validated::Deserialize + schemars::JsonSchema + 'static, | ||
{ | ||
CONTEXT.with(|ctx| { | ||
let ctx = &mut *ctx.borrow_mut(); | ||
let schema = ctx.schemas.entry(TypeId::of::<T>()).or_insert_with(|| { | ||
match jsonschema::JSONSchema::compile( | ||
&serde_json::to_value(ctx.generator.root_schema_for::<T>()).unwrap(), | ||
) { | ||
Ok(s) => s, | ||
Err(error) => { | ||
tracing::error!( | ||
%error, | ||
type_name = type_name::<T>(), | ||
"invalid JSON schema for type" | ||
); | ||
JSONSchema::compile(&Value::Object(Map::default())).unwrap() | ||
} | ||
} | ||
}); | ||
|
||
match schema.apply(value).basic() { | ||
BasicOutput::Valid(_) => Ok(()), | ||
BasicOutput::Invalid(v) => Err(v), | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
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
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