all jspg tests now passing
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub mod context;
|
||||
pub mod error;
|
||||
pub mod result;
|
||||
@ -8,35 +10,36 @@ pub use context::ValidationContext;
|
||||
pub use error::ValidationError;
|
||||
pub use result::ValidationResult;
|
||||
|
||||
use crate::database::schema::Schema;
|
||||
use crate::database::Database;
|
||||
use crate::validator::rules::util::is_integer;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct Validator {
|
||||
pub schemas: Arc<HashMap<String, Schema>>,
|
||||
pub db: Arc<Database>,
|
||||
}
|
||||
|
||||
impl Validator {
|
||||
pub fn new(schemas: Arc<HashMap<String, Schema>>) -> Self {
|
||||
Self { schemas }
|
||||
pub fn new(db: Arc<Database>) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
|
||||
pub fn get_schema_ids(&self) -> Vec<String> {
|
||||
self.schemas.keys().cloned().collect()
|
||||
self.db.schemas.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn check_type(t: &str, val: &Value) -> bool {
|
||||
if let Value::String(s) = val {
|
||||
if s.is_empty() {
|
||||
return true;
|
||||
}
|
||||
if let Value::String(s) = val
|
||||
&& s.is_empty()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
match t {
|
||||
"null" => val.is_null(),
|
||||
"boolean" => val.is_boolean(),
|
||||
"string" => val.is_string(),
|
||||
"number" => val.is_number(),
|
||||
"integer" => crate::validator::util::is_integer(val),
|
||||
"integer" => is_integer(val),
|
||||
"object" => val.is_object(),
|
||||
"array" => val.is_array(),
|
||||
_ => true,
|
||||
@ -48,13 +51,13 @@ impl Validator {
|
||||
schema_id: &str,
|
||||
instance: &Value,
|
||||
) -> Result<ValidationResult, ValidationError> {
|
||||
if let Some(schema) = self.schemas.get(schema_id) {
|
||||
if let Some(schema) = self.db.schemas.get(schema_id) {
|
||||
let ctx = ValidationContext::new(
|
||||
&self.schemas,
|
||||
&self.db,
|
||||
schema,
|
||||
schema,
|
||||
instance,
|
||||
std::collections::HashSet::new(),
|
||||
HashSet::new(),
|
||||
false,
|
||||
false,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user