-
-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use meta-schemas to validate input schemas #198
Comments
@aedeny is willing to work on this issue :) |
Hi @Stranger6667, can you please remind me how should the new validators be created from within the |
Hi @aedeny ! Thanks for asking! First, we got to reuse schemas as lazy_static::lazy_static! {
static ref DRAFT_4: serde_json::Value = serde_json::from_str(include_str!("../../meta_schemas/draft4.json")).expect("Valid schema!");
... // Similar with other schemas
static ref META_SCHEMAS: AHashMap<String, Value> = {
let mut store = AHashMap::with_capacity(3);
store.insert(
"http://json-schema.org/draft-04/schema".to_string(),
DRAFT_4
);
...
store
};
} Then validators could be compiled like this: lazy_static::lazy_static! {
static ref META_SCHEMA_VALIDATORS: AHashMap<draft::Draft, JSONSchema<'static>> = {
let mut store = AHashMap::with_capacity(3);
store.insert(
draft::Draft::Draft4,
JSONSchema::compile(DRAFT_4).expect("Valid metaschema")
);
...
store
};
} I didn't check whether the code above works, but it should be like this or similar (probably with some |
@Stranger6667 - Thanks for the help and explanation! I've applied your suggestions 😃 As I understand we should also change the return type of each validator's (e.g. pub(crate) type ValidationResult<'a> = Result<BoxedValidator, error::ValidationError<'a>>; And for the validators: impl MaxItemsValidator {
#[inline]
pub(crate) fn compile(schema: &Value) -> ValidationResult {
if let Some(limit) = schema.as_u64() {
Ok(Box::new(MaxItemsValidator { limit }))
} else {
Err(ValidationError...) // not sure what to do here
}
}
} Since we currently don't have context to populate the |
You're very welcome! :)
Exactly! :)
There is a special kind of pub(crate) fn schema(instance: &'a Value) -> ValidationError<'a> {
ValidationError {
instance_path: JSONPointer::default(),
instance: Cow::Borrowed(instance),
kind: ValidationErrorKind::Schema,
}
} |
If the input schema is invalid during the schema compilation process, then
CompilationError
is emitted. It has no useful info inside, which leads to a bad UX in such cases.It will be much better to use meta-schemas and existing
ValidationError
in such circumstances:lazy_static
;instance_path
will be available in those errors;schema_path
to validation errors in the future will benefit the compilation process automatically.Also, if meta-schemas will be sufficient, then
CompilationError
might be deleted. Even if not, then constructingValidationError
instead of them will provide much more useful output.But it is a backward-incompatible change, which still seems worth it.
Roughly required steps are:
lazy_static!
incompilation::options
. Probably it will require a slight refactoring there as meta-schemas are already included in the resulting binary - it will be nice to reuse thoseserde_json::Value
values. It should be fine to useexpect
there as those validators are known to be valid;compilation::options::CompilationOptions::compile
depending on the detected draft and the configuration logic from the previous step;compile
function toValidationError
;CompilationError
down in compilation logic - but I am not 100% sure about it. Maybe we can create some "InvalidSchema" variant toValidationError
for such cases when the meta schema didn't catch the error. I assume that it is either pretty rare or even not possible, so having one descriptive extra variant inValidationError
seems like a good option instead of havingunreachable!
orexpect
that are panicking.The text was updated successfully, but these errors were encountered: