Skip to content

Commit

Permalink
fix: jsonschema for dimension and remove default_config's jsonsschema…
Browse files Browse the repository at this point in the history
… check
  • Loading branch information
Pratik Mishra authored and Pratik Mishra committed Aug 5, 2024
1 parent 5233f1a commit 6777ba7
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
models::{Context, DefaultConfig},
schema::{contexts::dsl::contexts, default_configs::dsl},
},
helpers::{add_config_version, validate_jsonschema},
helpers::add_config_version,
};
use actix_web::{
delete, get, put,
Expand Down Expand Up @@ -137,11 +137,6 @@ async fn create(
last_modified_by: user.get_email(),
};

validate_jsonschema(
&state.default_config_validation_schema,
&default_config.schema,
)?;

let schema_compile_result = JSONSchema::options()
.with_draft(Draft::Draft7)
.compile(&default_config.schema);
Expand Down
104 changes: 1 addition & 103 deletions crates/context_aware_config/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,75 +30,6 @@ use superposition_types::{result as superposition, Cac, Condition, Overrides};

use std::collections::HashMap;

pub fn get_default_config_validation_schema() -> JSONSchema {
let my_schema = json!(
{
"type": "object",
"properties": {
"type": {
"anyOf": [
{
"type": "null"
},
{
"type": "string"
},
{
"type": "object"
},
{
"type": "number"
},
{
"type": "boolean"
},
{
"type": "array"
}
]
}
},
"required": [
"type"
],
"allOf": [
{
"if": {
"properties": {
"type": {
"const": "string"
}
}
},
"then": {
"oneOf": [
{
"required": ["pattern"],
"properties": { "pattern": { "type": "string" } }
},
{
"required": ["enum"],
"properties": {
"enum": {
"type": "array",
"contains": { "type": "string" },
"minContains": 1
},
}
}
]
}
}
// TODO: Add validations for Array types.
]
});

JSONSchema::options()
.with_draft(Draft::Draft7)
.compile(&my_schema)
.expect("THE IMPOSSIBLE HAPPENED, failed to compile the schema for the schema!")
}

pub fn parse_headermap_safe(headermap: &HeaderMap) -> HashMap<String, String> {
let mut req_headers = HashMap::new();
let record_header = |(header_name, header_val): (&HeaderName, &HeaderValue)| {
Expand All @@ -123,33 +54,10 @@ pub fn get_meta_schema() -> JSONSchema {
"type": "object",
"properties": {
"type": {
"enum": ["boolean", "number", "string"]
"enum": ["boolean", "number", "integer", "string", "array", "null"]
},
},
"required": ["type"],

// # Add extra validation if needed for other primitive data types
"if": {
"properties": { "type": { "const": "string" } }
}
, "then": {
"oneOf": [
{
"required": ["pattern"],
"properties": { "pattern": { "type": "string" } }
},
{
"required": ["enum"],
"properties": {
"enum": {
"type": "array",
"contains": { "type": "string" },
"minContains": 1
},
}
}
]
}
});

JSONSchema::options()
Expand Down Expand Up @@ -415,16 +323,6 @@ mod tests {
let ok_string_validation = x.validate(&ok_string_schema);
assert!(ok_string_validation.is_ok());

let error_string_schema = json!({"type": "string"});
let error_string_validation = x.validate(&error_string_schema).map_err(|e| {
let verrors = e.collect::<Vec<ValidationError>>();
format!(
"Error While validating string dataType, Bad schema: {:?}",
verrors.as_slice()
)
});
assert!(error_string_validation.is_err_and(|error| error.contains("Bad schema")));

let error_object_schema = json!({"type": "object"});
let error_object_validation = x.validate(&error_object_schema).map_err(|e| {
let verrors = e.collect::<Vec<ValidationError>>();
Expand Down
4 changes: 2 additions & 2 deletions crates/experimentation_platform/src/api/experiments/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct ConcludeExperimentRequest {

#[derive(Deserialize, Serialize, Clone)]
pub struct ContextPutReq {
pub context: serde_json::Map<String, Value>,
pub context: Map<String, Value>,
pub r#override: Value,
}

Expand Down Expand Up @@ -165,7 +165,7 @@ pub struct OverrideKeysUpdateRequest {

#[derive(Deserialize, Serialize, Clone)]
pub struct ContextMoveReq {
pub context: serde_json::Map<String, Value>,
pub context: Map<String, Value>,
}

/*********** List Audit API Filter Type **************/
Expand Down
1 change: 0 additions & 1 deletion crates/service_utils/src/service/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub struct AppState {
pub tenants: HashSet<String>,
pub cac_version: String,
pub db_pool: PgSchemaManager,
pub default_config_validation_schema: JSONSchema,
pub meta_schema: JSONSchema,
pub experimentation_flags: ExperimentationFlags,
pub snowflake_generator: Arc<Mutex<SnowflakeIdGenerator>>,
Expand Down
5 changes: 1 addition & 4 deletions crates/superposition/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use actix_web::dev::Service;
use actix_web::HttpMessage;
use actix_web::{web, web::get, web::scope, web::Data, App, HttpResponse, HttpServer};
use context_aware_config::api::*;
use context_aware_config::helpers::{
get_default_config_validation_schema, get_meta_schema,
};
use context_aware_config::helpers::get_meta_schema;
use experimentation_platform::api::*;
use std::sync::Arc;
use std::{collections::HashSet, io::Result};
Expand Down Expand Up @@ -144,7 +142,6 @@ async fn main() -> Result<()> {
.wrap(TenantMiddlewareFactory)
.app_data(Data::new(AppState {
db_pool: schema_manager.clone(),
default_config_validation_schema: get_default_config_validation_schema(),
cac_host: cac_host.to_owned(),
cac_version: cac_version.to_owned(),

Expand Down

0 comments on commit 6777ba7

Please sign in to comment.