Skip to content

Commit

Permalink
fix: fixed function template
Browse files Browse the repository at this point in the history
  • Loading branch information
mahatoankitkumar committed May 2, 2024
1 parent 761a69b commit 94751d0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/context_aware_config/src/api/functions/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ async fn test(
match result {
Ok(stdout) => Ok(HttpResponse::Ok()
.json(json!({"message": "Function validated the given value successfully", "stdout": stdout}))),
Err((e, stdout)) => Err(bad_argument!("Function validation failed with error: {}, stdout: {:?}", e, stdout )),
Err((e, stdout)) => Err(bad_argument!("Function validation failed with error: {}, stdout: {:?}", e, stdout.unwrap_or(String::new()))),
}
}

Expand Down
12 changes: 4 additions & 8 deletions crates/context_aware_config/src/validation_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ fn type_check_validate(code_str: &str) -> String {
)
}

fn execute_validate_fun(code_str: &str, val: Value) -> String {
fn execute_validate_fun(code_str: &str, value: Value, key: String) -> String {
format!(
r#"
const vm = require("node:vm")
const axios = require("./target/node_modules/axios")
const script = new vm.Script(\`
{}
Promise.resolve(validate({})).then((output) => {{
Promise.resolve(validate({}, {})).then((output) => {{
if(output!=true){{
throw new Error("The function did not return true as expected. Check the conditions or logic inside the function.")
Expand All @@ -44,7 +44,7 @@ fn execute_validate_fun(code_str: &str, val: Value) -> String {
script.runInNewContext({{axios,console,process}}, {{ timeout: 1500}});
"#,
code_str, val
code_str, value, key
)
}

Expand Down Expand Up @@ -107,11 +107,7 @@ pub fn execute_fn(
key: &str,
value: Value,
) -> Result<String, (String, Option<String>)> {
let fun_val = json!({
"key": key,
"value": value
});
let exec_code = execute_validate_fun(code_str, fun_val);
let exec_code = execute_validate_fun(code_str, value, format!("/{}/", key));
let output = Command::new("node")
.arg("-e")
.arg(generate_code(&exec_code))
Expand Down
3 changes: 2 additions & 1 deletion crates/frontend/src/pages/function/function_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub fn create_function_view() -> impl IntoView {
window.editor = monaco.editor.create(document.querySelector('.monaco'), {{
value: [
'async function validate() {{',
'async function validate(value, key) {{',
' return true;',
'}}'
].join('\n'),
language: 'javascript',
Expand Down

0 comments on commit 94751d0

Please sign in to comment.