-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 Continue working on schema validation
- Loading branch information
Showing
6 changed files
with
177 additions
and
10 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,3 +1,10 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use crate::schema_type::SchemaType; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Field { | ||
#[serde(rename = "type")] | ||
kind: SchemaType, | ||
label: String, | ||
description: Option<String>, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,51 @@ | ||
pub mod advanced_string_type; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use crate::schema_type::advanced_type::advanced_string_type::AdvancedStringType; | ||
use serde_json::Value; | ||
use thiserror::Error; | ||
use crate::schema_type::advanced_type::advanced_string_type::{AdvancedStringType, StringValidationError}; | ||
use crate::schema_type::SchemaType; | ||
use crate::traits::validator::Validator; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
#[serde(tag = "$", rename_all = "camelCase")] | ||
#[cfg_attr(test, derive(PartialEq))] | ||
pub enum AdvancedType { | ||
String(AdvancedStringType), | ||
Optional(Box<SchemaType>), | ||
} | ||
|
||
#[derive(Debug, PartialEq, Error)] | ||
pub enum AdvancedTypeValidationError { | ||
#[error("{0}")] | ||
StringValidationError(#[from] StringValidationError), | ||
} | ||
|
||
impl Validator for AdvancedType { | ||
type E = AdvancedTypeValidationError; | ||
|
||
fn validate(&self, value: &Value) -> Result<(), Self::E> { | ||
match self { | ||
AdvancedType::String(advanced_string) => Ok(advanced_string.validate(value)?), | ||
AdvancedType::Optional(nested_type) => { | ||
if let Value::Null = value { | ||
return Ok(()) | ||
} | ||
|
||
todo!() | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::schema_type::advanced_type::AdvancedType; | ||
use crate::schema_type::basic_type::BasicType; | ||
use crate::schema_type::SchemaType; | ||
|
||
#[test] | ||
fn optional_advanced_type_is_resolved_correctly() { | ||
let advanced_type = AdvancedType::Optional(Box::new(SchemaType::Basic(BasicType::String))); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ use thiserror::Error; | |
use uuid::{Error, Uuid}; | ||
use crate::traits::validator::Validator; | ||
|
||
#[derive(Debug, Error)] | ||
#[derive(Debug, PartialEq, Error)] | ||
pub enum BasicTypeValidationError { | ||
#[error("Incorrect type provided. Expected '{0}' but got '{1}'")] | ||
IncorrectType(BasicType, Value), | ||
|
@@ -19,9 +19,8 @@ pub enum BasicTypeValidationError { | |
IncorrectEmail(String), | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
#[cfg_attr(test, derive(PartialEq))] | ||
pub enum BasicType { | ||
String, | ||
Number, | ||
|
@@ -164,6 +163,7 @@ mod tests { | |
#[test] | ||
fn basic_email_type_is_validated_correctly() { | ||
assert!(BasicType::Email.validate(&json!("[email protected]")).is_ok()); | ||
assert!(BasicType::Email.validate(&json!("[email protected]")).is_ok()); | ||
|
||
assert!(BasicType::Email.validate(&json!("f1df9904-6f6b-4157-8a82-b1a566a50ec2")).is_err()); | ||
assert!(BasicType::Email.validate(&json!("")).is_err()); | ||
|