use super::core::console_log;
use super::types::{
create_validation_error, create_validation_result, JSONEvalWasm, ValidationError,
ValidationResult,
};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
impl JSONEvalWasm {
#[wasm_bindgen]
pub fn validate(
&mut self,
data: &str,
context: Option<String>,
) -> Result<ValidationResult, JsValue> {
let ctx = context.as_deref();
let token = self.reset_token();
match self.inner.validate(data, ctx, None, token.as_ref()) {
Ok(result) => {
let mut errors: std::collections::HashMap<String, ValidationError> =
std::collections::HashMap::new();
for (path, error) in result.errors {
errors.insert(path.clone(), create_validation_error(path.clone(), &error));
}
Ok(create_validation_result(result.has_error, errors))
}
Err(e) => {
let error_msg = format!("Validation failed: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
#[wasm_bindgen(js_name = validateJS)]
pub fn validate_js(&mut self, data: &str, context: Option<String>) -> Result<JsValue, JsValue> {
match self.validate_to_value(data, context, None) {
Ok(validation_result) => super::to_value(&validation_result).map_err(|e| {
let error_msg = format!("Failed to serialize validation result: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
JsValue::from_str(&error_msg)
}),
Err(e) => {
let error_msg = format!("Validation failed: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
}
impl JSONEvalWasm {
pub fn validate_to_value(
&mut self,
data: &str,
context: Option<String>,
paths: Option<Vec<String>>,
) -> Result<serde_json::Value, String> {
let ctx = context.as_deref();
let paths_ref = paths.as_ref().map(|v| v.as_slice());
let token = self.reset_token();
match self.inner.validate(data, ctx, paths_ref, token.as_ref()) {
Ok(result) => {
let mut errors_map = serde_json::Map::new();
for (path, error) in result.errors {
errors_map.insert(
path.clone(),
serde_json::json!({
"path": path,
"type": error.rule_type,
"message": error.message,
"code": error.code,
"pattern": error.pattern,
"fieldValue": error.field_value,
"data": error.data,
}),
);
}
Ok(serde_json::json!({
"has_error": result.has_error,
"error": errors_map,
}))
}
Err(e) => Err(e.to_string()),
}
}
}
#[wasm_bindgen]
impl JSONEvalWasm {
#[wasm_bindgen(js_name = validatePaths)]
pub fn validate_paths(
&mut self,
data: &str,
context: Option<String>,
paths: Option<Vec<String>>,
) -> Result<ValidationResult, JsValue> {
let ctx = context.as_deref();
let paths_ref = paths.as_ref().map(|v| v.as_slice());
let token = self.reset_token();
match self.inner.validate(data, ctx, paths_ref, token.as_ref()) {
Ok(result) => {
let mut errors: std::collections::HashMap<String, ValidationError> =
std::collections::HashMap::new();
for (path, error) in result.errors {
errors.insert(path.clone(), create_validation_error(path.clone(), &error));
}
Ok(create_validation_result(result.has_error, errors))
}
Err(e) => {
let error_msg = format!("Validation failed: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
#[wasm_bindgen(js_name = validatePathsJS)]
pub fn validate_paths_js(
&mut self,
data: &str,
context: Option<String>,
paths: Option<Vec<String>>,
) -> Result<JsValue, JsValue> {
match self.validate_to_value(data, context, paths) {
Ok(validation_result) => super::to_value(&validation_result).map_err(|e| {
let error_msg = format!("Failed to serialize validation result: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
JsValue::from_str(&error_msg)
}),
Err(e) => {
let error_msg = format!("Validation failed: {}", e);
console_log(&format!("[WASM ERROR] {}", error_msg));
Err(JsValue::from_str(&error_msg))
}
}
}
}