Skip to content
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

refactor: migrate to new version of tailcall-valid #3182

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 111 additions & 93 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ derive_more = { version = "1", features = ["from", "debug"] }
thiserror = "1.0.59"
url = { version = "2.5.0", features = ["serde"] }
convert_case = "0.6.0"
tailcall-valid = "0.1.1"
tailcall-valid = { path = "../tailcall-valid" }

[dependencies]
# dependencies specific to CLI must have optional = true and the dep should be added to default feature.
Expand Down
15 changes: 8 additions & 7 deletions src/cli/generator/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ pub struct Schema {
pub mutation: Option<String>,
}

fn between(threshold: f32, min: f32, max: f32) -> Valid<(), String> {
Valid::<(), String>::fail(format!(
fn between(threshold: f32, min: f32, max: f32) -> Valid<(), String, String> {
Valid::<(), String, String>::fail(format!(
"Invalid threshold value ({:.2}). Allowed range is [{:.2} - {:.2}] inclusive.",
threshold, min, max
))
Expand All @@ -126,7 +126,8 @@ fn between(threshold: f32, min: f32, max: f32) -> Valid<(), String> {

impl ValidateFrom<PresetConfig> for Preset {
type Error = String;
fn validate_from(config: PresetConfig) -> Valid<Self, Self::Error> {
type Trace = String;
fn validate_from(config: PresetConfig) -> Valid<Self, Self::Error, Self::Trace> {
let mut preset = Preset::new();

if let Some(merge_type) = config.merge_type {
Expand Down Expand Up @@ -268,7 +269,7 @@ mod tests {
use std::collections::HashMap;

use pretty_assertions::assert_eq;
use tailcall_valid::{ValidateInto, ValidationError, Validator};
use tailcall_valid::{Cause, ValidateInto, Validator};

use super::*;

Expand Down Expand Up @@ -333,7 +334,7 @@ mod tests {
unwrap_single_field_types: None,
};

let transform_preset: Result<Preset, ValidationError<String>> =
let transform_preset: Result<Preset, Cause<String>, String> =
config_preset.validate_into().to_result();
assert!(transform_preset.is_err());
}
Expand Down Expand Up @@ -434,7 +435,7 @@ mod tests {
let json = r#"
{"output": {
"paths": "./output.graphql",
}}
}}
"#;
let expected_error =
"unknown field `paths`, expected `path` or `format` at line 3 column 21";
Expand All @@ -446,7 +447,7 @@ mod tests {
let json = r#"
{"schema": {
"querys": "Query",
}}
}}
"#;
let expected_error =
"unknown field `querys`, expected `query` or `mutation` at line 3 column 22";
Expand Down
54 changes: 24 additions & 30 deletions src/core/blueprint/cors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use derive_setters::Setters;
use http::header::{self, HeaderName, HeaderValue, InvalidHeaderValue};
use http::request::Parts;
use tailcall_valid::ValidationError;
use tailcall_valid::Cause;

use super::BlueprintError;
use crate::core::config;
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Cors {

fn ensure_usable_cors_rules(
layer: &Cors,
) -> Result<(), ValidationError<crate::core::blueprint::BlueprintError>> {
) -> Result<(), Cause<crate::core::blueprint::BlueprintError, String>> {
if layer.allow_credentials {
let allowing_all_headers = layer
.allow_headers
Expand All @@ -127,11 +127,9 @@ fn ensure_usable_cors_rules(
.is_some();

if allowing_all_headers {
return Err(ValidationError::new(
BlueprintError::InvalidCORSConfiguration(
"Access-Control-Allow-Headers".to_string(),
),
));
return Err(Cause::new(BlueprintError::InvalidCORSConfiguration(
"Access-Control-Allow-Headers".to_string(),
)));
}

let allowing_all_methods = layer
Expand All @@ -141,51 +139,47 @@ fn ensure_usable_cors_rules(
.is_some();

if allowing_all_methods {
return Err(ValidationError::new(
BlueprintError::InvalidCORSConfiguration(
"Access-Control-Allow-Methods".to_string(),
),
));
return Err(Cause::new(BlueprintError::InvalidCORSConfiguration(
"Access-Control-Allow-Methods".to_string(),
)));
}

let allowing_all_origins = layer.allow_origins.iter().any(is_wildcard);

if allowing_all_origins {
return Err(ValidationError::new(
BlueprintError::InvalidCORSConfiguration("Access-Control-Allow-Origin".to_string()),
));
return Err(Cause::new(BlueprintError::InvalidCORSConfiguration(
"Access-Control-Allow-Origin".to_string(),
)));
}

if layer.expose_headers_is_wildcard() {
return Err(ValidationError::new(
BlueprintError::InvalidCORSConfiguration(
"Access-Control-Expose-Headers".to_string(),
),
));
return Err(Cause::new(BlueprintError::InvalidCORSConfiguration(
"Access-Control-Expose-Headers".to_string(),
)));
}
}
Ok(())
}

impl TryFrom<config::cors::Cors> for Cors {
type Error = ValidationError<crate::core::blueprint::BlueprintError>;
type Error = Cause<crate::core::blueprint::BlueprintError, String>;

fn try_from(
value: config::cors::Cors,
) -> Result<Self, ValidationError<crate::core::blueprint::BlueprintError>> {
) -> Result<Self, Cause<crate::core::blueprint::BlueprintError, String>> {
let cors = Cors {
allow_credentials: value.allow_credentials.unwrap_or_default(),
allow_headers: (!value.allow_headers.is_empty()).then_some(
value
.allow_headers
.join(", ")
.parse()
.map_err(|e: InvalidHeaderValue| ValidationError::new(e.into()))?,
.map_err(|e: InvalidHeaderValue| Cause::new(e.into()))?,
),
allow_methods: {
Some(if value.allow_methods.is_empty() {
"*".parse()
.map_err(|e: InvalidHeaderValue| ValidationError::new(e.into()))?
.map_err(|e: InvalidHeaderValue| Cause::new(e.into()))?
} else {
value
.allow_methods
Expand All @@ -194,34 +188,34 @@ impl TryFrom<config::cors::Cors> for Cors {
.collect::<Vec<String>>()
.join(", ")
.parse()
.map_err(|e: InvalidHeaderValue| ValidationError::new(e.into()))?
.map_err(|e: InvalidHeaderValue| Cause::new(e.into()))?
})
},
allow_origins: value
.allow_origins
.into_iter()
.map(|val| {
val.parse()
.map_err(|e: InvalidHeaderValue| ValidationError::new(e.into()))
.map_err(|e: InvalidHeaderValue| Cause::new(e.into()))
})
.collect::<Result<_, ValidationError<crate::core::blueprint::BlueprintError>>>()?,
.collect::<Result<_, Cause<crate::core::blueprint::BlueprintError, String>>>()?,
allow_private_network: value.allow_private_network.unwrap_or_default(),
expose_headers: Some(
value
.expose_headers
.join(", ")
.parse()
.map_err(|e: InvalidHeaderValue| ValidationError::new(e.into()))?,
.map_err(|e: InvalidHeaderValue| Cause::new(e.into()))?,
),
max_age: value.max_age.map(|val| val.into()),
vary: value
.vary
.iter()
.map(|val| {
val.parse()
.map_err(|e: InvalidHeaderValue| ValidationError::new(e.into()))
.map_err(|e: InvalidHeaderValue| Cause::new(e.into()))
})
.collect::<Result<_, ValidationError<crate::core::blueprint::BlueprintError>>>()?,
.collect::<Result<_, Cause<crate::core::blueprint::BlueprintError, String>>>()?,
};
ensure_usable_cors_rules(&cors)?;
Ok(cors)
Expand Down
Loading