Skip to content

Commit

Permalink
Validate default values for slots
Browse files Browse the repository at this point in the history
  • Loading branch information
andriygm committed Oct 16, 2024
1 parent 954768b commit 8aebca5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cli/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{error::Error, path::PathBuf, process::exit, time::Instant};
use colored::Colorize;
use spackle::core::{
config::Config,
slot,
template::{self, ValidateError},
};

Expand All @@ -19,15 +20,15 @@ pub fn run(project_dir: &PathBuf, config: &Config) {
match e {
ValidateError::TeraError(e) => {
eprintln!(
" {}\n {}\n",
"{}\n{}\n",
"❌ Error validating template files".bright_red(),
e.to_string().red()
);
}
ValidateError::RenderError(e) => {
for (templ, e) in e {
eprintln!(
" {}\n {}\n",
"{}\n{}\n",
format!("❌ Template {} has errors", templ.bright_red().bold())
.bright_red(),
e.source().map(|e| e.to_string()).unwrap_or_default().red()
Expand All @@ -41,6 +42,20 @@ pub fn run(project_dir: &PathBuf, config: &Config) {
}
}

match slot::validate(&config.slots) {
Ok(()) => {
println!(" 👌 {}\n", "Slot data is valid".bright_green());
}
Err(e) => {
eprintln!(
"{}\n{}\n",
"❌ Error validating slot configuration".bright_red(),
e.to_string().red()
);
exit(1);
}
}

print_elapsed_time(start_time);
}

Expand Down
24 changes: 24 additions & 0 deletions src/core/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ impl Slot {
}
}

pub fn validate(slots: &Vec<Slot>) -> Result<(), Error> {
for slot in slots {
if let Some(default_value) = &slot.default {
match slot.r#type {
SlotType::String => {
// String always valid, no need to check
}
SlotType::Number => {
if default_value.parse::<f64>().is_err() {
return Err(Error::TypeMismatch(slot.key.clone(), "number".to_string()));
}
}
SlotType::Boolean => {
if default_value.parse::<bool>().is_err() {
return Err(Error::TypeMismatch(slot.key.clone(), "boolean".to_string()));
}
}
}
}
}

Ok(())
}

pub fn validate_data(data: &HashMap<String, String>, slots: &Vec<Slot>) -> Result<(), Error> {
for entry in data.iter() {
// Check if the data is assigned to a slot
Expand Down
21 changes: 21 additions & 0 deletions tests/data/bad_default_slot_val/spackle.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

[[slots]]
key = "default_string"
type = "String"
name = "Default string"
description = "Default string"
default = "default"

[[slots]]
key = "default_number"
type = "Number"
name = "Default number"
description = "Default number"
default = "notanumber"

[[slots]]
key = "default_boolean"
type = "Boolean"
name = "Default boolean"
description = "Default boolean"
default = "invalid"
3 changes: 3 additions & 0 deletions tests/data/bad_default_slot_val/test.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{ default_string }}
{{ default_number }}
{{ default_boolean }}

0 comments on commit 8aebca5

Please sign in to comment.