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

initial validation #3

Merged
merged 2 commits into from
Feb 26, 2024
Merged
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nixEnvSelector.nixFile": "${workspaceFolder}/flake.nix"
}
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ opt-level = "z"

[workspace]
members = ["dnas/*/zomes/coordinator/*", "dnas/*/zomes/integrity/*"]
resolver = "2"

[workspace.dependencies]
hdi = "=0.3.6"
hdk = "=0.2.6"
serde = { version = "=1.0.166", features = ["derive"] }
serde_json = "1"
alloy-primitives = { version = "0.6.3", features = ["serde"] }
hc_zome_profiles_coordinator = { git="https://github.com/holochain-open-dev/profiles.git", rev="a1a487c8d6a8fd9910ba9b3f26e47df0bf0d09ae" }
hc_zome_profiles_integrity = { git="https://github.com/holochain-open-dev/profiles.git", rev="a1a487c8d6a8fd9910ba9b3f26e47df0bf0d09ae" }
hc_zome_profiles_coordinator = { git = "https://github.com/holochain-open-dev/profiles.git", rev = "a1a487c8d6a8fd9910ba9b3f26e47df0bf0d09ae" }
hc_zome_profiles_integrity = { git = "https://github.com/holochain-open-dev/profiles.git", rev = "a1a487c8d6a8fd9910ba9b3f26e47df0bf0d09ae" }

[workspace.dependencies.grants]
path = "dnas/grant_pools/zomes/coordinator/grants"
Expand Down
3 changes: 2 additions & 1 deletion dnas/grant_pools/zomes/integrity/grants/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ name = "grants_integrity"
hdi = { workspace = true }

serde = { workspace = true }
serde_json = { workspace = true }

alloy-primitives = { workspace = true }
alloy-primitives = { workspace = true }
50 changes: 26 additions & 24 deletions dnas/grant_pools/zomes/integrity/grants/src/application_template.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
use hdi::prelude::*;

#[hdk_entry_helper]
#[derive(Clone, PartialEq)]
pub struct ApplicationTemplate {
pub json_schema: String,
}
pub fn validate_create_application_template(
_action: EntryCreationAction,
_application_template: ApplicationTemplate,
application_template: ApplicationTemplate,
) -> ExternResult<ValidateCallbackResult> {
Ok(ValidateCallbackResult::Valid)
let valid_json: Result<String, serde_json::Error> =
serde_json::from_str(&application_template.json_schema);
if valid_json.is_err() {
Ok(ValidateCallbackResult::Invalid(
"Schema not valid json".to_string(),
))
} else {
Ok(ValidateCallbackResult::Valid)
}
}
pub fn validate_update_application_template(
_action: Update,
_application_template: ApplicationTemplate,
_original_action: EntryCreationAction,
_original_application_template: ApplicationTemplate,
) -> ExternResult<ValidateCallbackResult> {
Ok(
ValidateCallbackResult::Invalid(
String::from("Application Templates cannot be updated"),
),
)
Ok(ValidateCallbackResult::Invalid(String::from(
"Application Templates cannot be updated",
)))
}
pub fn validate_delete_application_template(
_action: Delete,
_original_action: EntryCreationAction,
_original_application_template: ApplicationTemplate,
) -> ExternResult<ValidateCallbackResult> {
Ok(
ValidateCallbackResult::Invalid(
String::from("Application Templates cannot be deleted"),
),
)
Ok(ValidateCallbackResult::Invalid(String::from(
"Application Templates cannot be deleted",
)))
}
pub fn validate_create_link_all_application_templates(
_action: CreateLink,
_base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let action_hash = target_address
.into_action_hash()
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("No action hash associated with link"))
),
)?;
let action_hash =
target_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"No action hash associated with link"
))))?;
let record = must_get_valid_record(action_hash)?;
let _application_template: crate::ApplicationTemplate = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Linked action must reference an entry"))
),
)?;
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_all_application_templates(
Expand Down
91 changes: 64 additions & 27 deletions dnas/grant_pools/zomes/integrity/grants/src/evaluation_template.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,69 @@
use hdi::prelude::*;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct NumberRange {
min: u32,
max: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct WeightedCriteria {
label: String,
weight: u32,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum QuantitativeRating {
Single(u32),
Single(NumberRange),
Weighted(Vec<WeightedCriteria>),
}

#[hdk_entry_helper]
#[derive(Clone, PartialEq)]
#[derive(Clone)]
pub struct EvaluationTemplate {
pub qualitative_json_schema: String,
pub quantitative_rating: QuantitativeRating,
}
pub fn validate_create_evaluation_template(
_action: EntryCreationAction,
_evaluation_template: EvaluationTemplate,
evaluation_template: EvaluationTemplate,
) -> ExternResult<ValidateCallbackResult> {
let valid_json: Result<String, serde_json::Error> =
serde_json::from_str(&evaluation_template.qualitative_json_schema);
if valid_json.is_err() {
return Ok(ValidateCallbackResult::Invalid(
"Schema not valid json".to_string(),
));
}
match evaluation_template.quantitative_rating {
QuantitativeRating::Single(range) => {
if range.max < range.min {
return Ok(ValidateCallbackResult::Invalid(
"Max must be greater than min".to_string(),
));
}
}
QuantitativeRating::Weighted(weighted_criteria) => {
if weighted_criteria.len() < 2 {
return Ok(ValidateCallbackResult::Invalid(
"Must have more than one weighted criteria".to_string(),
));
}
for criteria in weighted_criteria {
if criteria.label.is_empty() {
return Ok(ValidateCallbackResult::Invalid(
"Label can't be empty".to_string(),
));
}
if criteria.weight == 0 {
return Ok(ValidateCallbackResult::Invalid(
"Weight cannot be zero".to_string(),
));
}
}
}
}
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_update_evaluation_template(
Expand All @@ -28,46 +72,39 @@ pub fn validate_update_evaluation_template(
_original_action: EntryCreationAction,
_original_evaluation_template: EvaluationTemplate,
) -> ExternResult<ValidateCallbackResult> {
Ok(
ValidateCallbackResult::Invalid(
String::from("Evaluation Templates cannot be updated"),
),
)
Ok(ValidateCallbackResult::Invalid(String::from(
"Evaluation Templates cannot be updated",
)))
}
pub fn validate_delete_evaluation_template(
_action: Delete,
_original_action: EntryCreationAction,
_original_evaluation_template: EvaluationTemplate,
) -> ExternResult<ValidateCallbackResult> {
Ok(
ValidateCallbackResult::Invalid(
String::from("Evaluation Templates cannot be deleted"),
),
)
Ok(ValidateCallbackResult::Invalid(String::from(
"Evaluation Templates cannot be deleted",
)))
}
pub fn validate_create_link_all_evaluation_templates(
_action: CreateLink,
_base_address: AnyLinkableHash,
target_address: AnyLinkableHash,
_tag: LinkTag,
) -> ExternResult<ValidateCallbackResult> {
let action_hash = target_address
.into_action_hash()
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("No action hash associated with link"))
),
)?;
let action_hash =
target_address
.into_action_hash()
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"No action hash associated with link"
))))?;
let record = must_get_valid_record(action_hash)?;
let _evaluation_template: crate::EvaluationTemplate = record
.entry()
.to_app_option()
.map_err(|e| wasm_error!(e))?
.ok_or(
wasm_error!(
WasmErrorInner::Guest(String::from("Linked action must reference an entry"))
),
)?;
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from(
"Linked action must reference an entry"
))))?;
Ok(ValidateCallbackResult::Valid)
}
pub fn validate_delete_link_all_evaluation_templates(
Expand Down
Loading
Loading