refactor(yahweh): Fase 2 — extraer helpers puros a yahweh-meta-runtime
Sigue de la Fase 1 (lift del schema). Ahora los helpers puros que cualquier widget renderer o backend ejecutor consume sobre el schema viven en yahweh-meta-runtime. Sin GPUI, sin nakui — usa cierres en lugar de traits para decoupling máximo. Crate nuevo crates/modules/ui_engine/libs/meta-runtime: - parse.rs: parse_field_value, infer_param_value, resolve_param_value. - delta.rs: compute_field_delta, compute_clear_fields. - refs.rs: validate_entity_refs(load: F, refs) con cierre Fn(&str, Uuid) -> Option<Value> en vez de trait Store. - format.rs: human_label_for_record, render_value, value_to_input_text, short_uuid. - 33 tests propios. nakui-ui: - Nueva dep yahweh-meta-runtime. - Borrado código local equivalente (~200 líneas) + 34 tests duplicados. - validate_entity_refs callsite usa cierre: validate_entity_refs(|e, id| store.load(e, id), &refs). - 14 tests runtime-específicos quedan (compact/snapshot/event-log/ morphism pipeline/load_ui_modules). Distribución tests: 48 → 14 nakui-ui; +33 yahweh-meta-runtime. Cada crate afectado builds + tests limpio individualmente. Workspace build full no completó esta corrida por OOM al compilar surrealdb-core (ambiental, no relacionado). Fase 2b pendiente: extraer render widgets (form/list/modal/ EntityRef selector) a yahweh — requiere diseñar MetaBackend trait. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
//! Cálculo del delta entre el record actual y la propuesta del form.
|
||||
//!
|
||||
//! Sirve a un runtime de edición para emitir SOLO los Set/Clear que
|
||||
//! cambian algo: log + apply minimales, no-op edits = 0 entries.
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
/// Calcula el delta entre el record actual y los valores propuestos
|
||||
/// del form. Devuelve un Map con sólo los campos cuyo valor difiere.
|
||||
///
|
||||
/// Comparación: igualdad estructural sobre `serde_json::Value`. Un
|
||||
/// `current=Value::Null` (record no encontrado) hace que todos los
|
||||
/// campos del `proposed` sean considerados nuevos. Un campo del
|
||||
/// proposed que coincide con el del current se omite. Campos que
|
||||
/// están en current pero NO en proposed se preservan tal cual (el
|
||||
/// edit no los toca; ver [`compute_clear_fields`] para borrar
|
||||
/// explícito desde un input vacío).
|
||||
pub fn compute_field_delta(
|
||||
current: &Value,
|
||||
proposed: &serde_json::Map<String, Value>,
|
||||
) -> serde_json::Map<String, Value> {
|
||||
proposed
|
||||
.iter()
|
||||
.filter(|(field, value)| current.get(field.as_str()) != Some(*value))
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Decide cuáles fields del `to_clear` candidate list ameritan
|
||||
/// realmente un `FieldOp::Clear`: sólo los que existen en el current
|
||||
/// con un valor non-null. Para fields ausentes o ya null, Clear es
|
||||
/// no-op semántico (el post-state es el mismo) y dropearlos
|
||||
/// preserva la propiedad "1 op = 1 cambio efectivo" del log.
|
||||
///
|
||||
/// Preserva el orden del input para que el log entry sea estable.
|
||||
pub fn compute_clear_fields(current: &Value, to_clear: &[String]) -> Vec<String> {
|
||||
to_clear
|
||||
.iter()
|
||||
.filter(|f| match current.get(f.as_str()) {
|
||||
None | Some(Value::Null) => false,
|
||||
Some(_) => true,
|
||||
})
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
fn map(items: &[(&str, Value)]) -> serde_json::Map<String, Value> {
|
||||
items.iter().map(|(k, v)| (k.to_string(), v.clone())).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_empty_when_all_fields_match() {
|
||||
let current = json!({"name": "Acme", "saldo": 100_i64, "currency": "USD"});
|
||||
let proposed = map(&[
|
||||
("name", json!("Acme")),
|
||||
("saldo", json!(100_i64)),
|
||||
("currency", json!("USD")),
|
||||
]);
|
||||
assert!(compute_field_delta(¤t, &proposed).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_includes_only_changed_field() {
|
||||
let current = json!({"name": "Acme", "saldo": 100_i64});
|
||||
let proposed = map(&[("name", json!("Acme")), ("saldo", json!(200_i64))]);
|
||||
let d = compute_field_delta(¤t, &proposed);
|
||||
assert_eq!(d.len(), 1);
|
||||
assert_eq!(d.get("saldo"), Some(&json!(200_i64)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_treats_missing_record_as_all_new() {
|
||||
let current = Value::Null;
|
||||
let proposed = map(&[("name", json!("Acme")), ("saldo", json!(0_i64))]);
|
||||
assert_eq!(compute_field_delta(¤t, &proposed).len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_distinguishes_int_from_string_repr() {
|
||||
let current = json!({"qty": 100_i64});
|
||||
let proposed = map(&[("qty", json!(100_i64))]);
|
||||
assert!(compute_field_delta(¤t, &proposed).is_empty());
|
||||
|
||||
let current_str = json!({"qty": "100"});
|
||||
let proposed_int = map(&[("qty", json!(100_i64))]);
|
||||
assert_eq!(compute_field_delta(¤t_str, &proposed_int).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delta_skips_fields_absent_from_proposed() {
|
||||
let current = json!({"name": "Acme", "saldo": 100_i64, "extra": "x"});
|
||||
let proposed = map(&[("name", json!("Acme")), ("saldo", json!(150_i64))]);
|
||||
let d = compute_field_delta(¤t, &proposed);
|
||||
assert_eq!(d.len(), 1);
|
||||
assert!(!d.contains_key("extra"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_fields_skips_absent_and_null() {
|
||||
let current = json!({"name": "Acme", "notes": "lorem", "tag": null});
|
||||
let to_clear = vec![
|
||||
"name".into(),
|
||||
"notes".into(),
|
||||
"tag".into(),
|
||||
"missing".into(),
|
||||
];
|
||||
assert_eq!(
|
||||
compute_clear_fields(¤t, &to_clear),
|
||||
vec!["name".to_string(), "notes".to_string()]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_fields_preserves_input_order() {
|
||||
let current = json!({"a": 1, "b": 2, "c": 3});
|
||||
let to_clear = vec!["c".into(), "a".into(), "b".into()];
|
||||
assert_eq!(
|
||||
compute_clear_fields(¤t, &to_clear),
|
||||
vec!["c", "a", "b"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clear_fields_empty_when_current_is_null() {
|
||||
let current = Value::Null;
|
||||
let to_clear = vec!["name".into()];
|
||||
assert!(compute_clear_fields(¤t, &to_clear).is_empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
//! Helpers de presentación humana para records y values.
|
||||
//!
|
||||
//! Sin GPUI: devuelven `String`s. El widget renderer los wrap-ea
|
||||
//! en `div().child(...)` o equivalente.
|
||||
|
||||
use serde_json::Value;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Etiqueta humana para representar un record en el selector de
|
||||
/// EntityRef. Heurística: prefiere campos comunes en este orden:
|
||||
/// `name`, `label`, `title`, `sku`, `sku_id`. Fallback al UUID corto.
|
||||
pub fn human_label_for_record(value: &Value, id: &Uuid) -> String {
|
||||
for key in ["name", "label", "title", "sku", "sku_id"] {
|
||||
if let Some(v) = value.get(key).and_then(Value::as_str) {
|
||||
if !v.is_empty() {
|
||||
return format!("{} ({})", v, short_uuid(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
short_uuid(id)
|
||||
}
|
||||
|
||||
/// Render legible de un `Value` arbitrario para mostrar en una celda
|
||||
/// de lista. Strings van pelados; bools como ✓/✗; el resto via
|
||||
/// `Display`.
|
||||
pub fn render_value(v: Option<&Value>) -> String {
|
||||
match v {
|
||||
None | Some(Value::Null) => String::new(),
|
||||
Some(Value::String(s)) => s.clone(),
|
||||
Some(Value::Bool(b)) => if *b { "✓" } else { "✗" }.to_string(),
|
||||
Some(Value::Number(n)) => n.to_string(),
|
||||
Some(other) => other.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Conversión inversa a `parse_field_value`: del JSON al texto raw
|
||||
/// que un input puede tomar y volver a parsearse igual al submit.
|
||||
/// Usado para pre-llenar inputs en modo edit.
|
||||
pub fn value_to_input_text(v: &Value) -> String {
|
||||
match v {
|
||||
Value::Null => String::new(),
|
||||
Value::String(s) => s.clone(),
|
||||
Value::Bool(b) => if *b { "true" } else { "false" }.to_string(),
|
||||
Value::Number(n) => n.to_string(),
|
||||
other => other.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Primeros 8 chars del UUID en forma canónica. Útil para logs y UI
|
||||
/// donde el UUID full es ruido visual.
|
||||
pub fn short_uuid(id: &Uuid) -> String {
|
||||
id.to_string().chars().take(8).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn human_label_prefers_name_over_id() {
|
||||
let id = Uuid::new_v4();
|
||||
let v = json!({"name": "Acme S.A.", "email": "x@y.z"});
|
||||
let label = human_label_for_record(&v, &id);
|
||||
assert!(label.starts_with("Acme S.A."));
|
||||
assert!(label.contains(&short_uuid(&id)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn human_label_falls_back_through_label_title_sku() {
|
||||
let id = Uuid::new_v4();
|
||||
let only_label = json!({"label": "X"});
|
||||
assert!(human_label_for_record(&only_label, &id).starts_with("X "));
|
||||
let only_title = json!({"title": "Y"});
|
||||
assert!(human_label_for_record(&only_title, &id).starts_with("Y "));
|
||||
let only_sku = json!({"sku": "Z"});
|
||||
assert!(human_label_for_record(&only_sku, &id).starts_with("Z "));
|
||||
let only_sku_id = json!({"sku_id": "W"});
|
||||
assert!(human_label_for_record(&only_sku_id, &id).starts_with("W "));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn human_label_falls_back_to_short_uuid_when_no_keys_match() {
|
||||
let id = Uuid::new_v4();
|
||||
let v = json!({"random": "field"});
|
||||
assert_eq!(human_label_for_record(&v, &id), short_uuid(&id));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_value_handles_basic_kinds() {
|
||||
assert_eq!(render_value(None), "");
|
||||
assert_eq!(render_value(Some(&Value::Null)), "");
|
||||
assert_eq!(render_value(Some(&json!("hola"))), "hola");
|
||||
assert_eq!(render_value(Some(&json!(true))), "✓");
|
||||
assert_eq!(render_value(Some(&json!(false))), "✗");
|
||||
assert_eq!(render_value(Some(&json!(42))), "42");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_to_input_text_round_trip_with_strings_and_numbers() {
|
||||
assert_eq!(value_to_input_text(&Value::Null), "");
|
||||
assert_eq!(value_to_input_text(&json!("x")), "x");
|
||||
assert_eq!(value_to_input_text(&json!(true)), "true");
|
||||
assert_eq!(value_to_input_text(&json!(false)), "false");
|
||||
assert_eq!(value_to_input_text(&json!(42)), "42");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_uuid_returns_first_8_chars() {
|
||||
let id = Uuid::parse_str("01ARZ3ND-EKTS-V4RR-FFQ6-9G5FAV000000").ok();
|
||||
// Si el parse falla, usamos uno fresco — el invariant es la
|
||||
// longitud, no el contenido.
|
||||
let id = id.unwrap_or_else(Uuid::new_v4);
|
||||
assert_eq!(short_uuid(&id).len(), 8);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//! `yahweh-meta-runtime` — helpers puros para runtimes metainterfaz.
|
||||
//!
|
||||
//! Consume [`yahweh_meta_schema`] (los tipos `Module`/`View`/`FieldSpec`/
|
||||
//! `FieldKind`/`Action`/etc.) y aporta funciones puras que cualquier
|
||||
//! widget renderer o backend ejecutor necesita:
|
||||
//!
|
||||
//! - **Parse**: convertir el texto de un input a `serde_json::Value`
|
||||
//! tipado según el `FieldKind` del spec.
|
||||
//! - **Delta**: calcular qué cambió entre el estado actual y la
|
||||
//! propuesta del form (Set + Clear).
|
||||
//! - **Validation**: verificar que cada EntityRef apunte a un record
|
||||
//! que existe (toma cierre `load`, no trait).
|
||||
//! - **Format**: presentación humana de records (label heurístico,
|
||||
//! render de values, UUID corto, round-trip a input text).
|
||||
//!
|
||||
//! Sin GPUI, sin acoplamiento a un backend específico. Cualquier
|
||||
//! implementación de store/log puede consumirlos.
|
||||
//!
|
||||
//! El widget render (form/list/modal) vive en otro crate yahweh
|
||||
//! que esto consume; el runtime concreto (`nakui-ui`) implementa la
|
||||
//! conexión a su event-log/executor y compone ambos.
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
pub mod delta;
|
||||
pub mod format;
|
||||
pub mod parse;
|
||||
pub mod refs;
|
||||
|
||||
pub use delta::{compute_clear_fields, compute_field_delta};
|
||||
pub use format::{human_label_for_record, render_value, short_uuid, value_to_input_text};
|
||||
pub use parse::{infer_param_value, parse_field_value, resolve_param_value};
|
||||
pub use refs::validate_entity_refs;
|
||||
@@ -0,0 +1,231 @@
|
||||
//! Parseo de inputs del form a `serde_json::Value` tipado.
|
||||
|
||||
use serde_json::{json, Value};
|
||||
use uuid::Uuid;
|
||||
|
||||
use yahweh_meta_schema::{FieldKind, FieldSpec};
|
||||
|
||||
/// Convierte el texto raw de un input al `Value` tipado según el
|
||||
/// `kind` del spec.
|
||||
///
|
||||
/// - `Text` / `Multiline` / `Date` → string passthrough.
|
||||
/// - `EntityRef` → string del UUID **trimmed**, validado como UUID
|
||||
/// parseable. Falla con mensaje claro si no parsea.
|
||||
/// - `Boolean` → variantes comunes (`true/yes/1/on/y` y `false/no/0/off/n`).
|
||||
/// - `Number` → i64 si parsea, sino f64.
|
||||
pub fn parse_field_value(kind: FieldKind, raw: &str) -> Result<Value, String> {
|
||||
match kind {
|
||||
FieldKind::Text | FieldKind::Multiline | FieldKind::Date => Ok(json!(raw)),
|
||||
// EntityRef se almacena como string del UUID seleccionado.
|
||||
// El selector clickable garantiza UUIDs válidos en happy
|
||||
// path; este check protege paste manual o garbage tipeado.
|
||||
FieldKind::EntityRef => {
|
||||
let trimmed = raw.trim();
|
||||
Uuid::parse_str(trimmed).map_err(|_| {
|
||||
format!("'{raw}' no es UUID válido (usá el selector de records)")
|
||||
})?;
|
||||
Ok(json!(trimmed))
|
||||
}
|
||||
FieldKind::Boolean => match raw.to_ascii_lowercase().as_str() {
|
||||
"true" | "yes" | "1" | "on" | "y" => Ok(json!(true)),
|
||||
"" | "false" | "no" | "0" | "off" | "n" => Ok(json!(false)),
|
||||
other => Err(format!("'{other}' no es booleano")),
|
||||
},
|
||||
FieldKind::Number => {
|
||||
if let Ok(i) = raw.parse::<i64>() {
|
||||
Ok(json!(i))
|
||||
} else if let Ok(f) = raw.parse::<f64>() {
|
||||
Ok(json!(f))
|
||||
} else {
|
||||
Err(format!("'{raw}' no es número"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resuelve un param de morphism a su `Value` según el `FieldSpec`
|
||||
/// del form. **Strict path**: si hay spec, valida `required` y parsea
|
||||
/// con el `kind` declarado (ej. Boolean rebota con "abc" antes de
|
||||
/// llegar al morphism). **Fallback path**: si no hay spec (param
|
||||
/// declarado en `Action::Morphism.params` que no aparece en
|
||||
/// `form.fields`), usa la heurística [`infer_param_value`] para no
|
||||
/// quedar atado a un schema mal-formado.
|
||||
///
|
||||
/// Errores tienen el label legible del spec, así el toast de la UI
|
||||
/// es interpretable.
|
||||
pub fn resolve_param_value(
|
||||
field_name: &str,
|
||||
raw: &str,
|
||||
spec: Option<&FieldSpec>,
|
||||
) -> Result<Value, String> {
|
||||
let Some(s) = spec else {
|
||||
return Ok(infer_param_value(raw));
|
||||
};
|
||||
|
||||
let label = if s.label.is_empty() { field_name } else { &s.label };
|
||||
|
||||
if s.required && raw.trim().is_empty() {
|
||||
return Err(format!("param '{label}' es obligatorio y está vacío"));
|
||||
}
|
||||
if raw.is_empty() && !s.required {
|
||||
return Ok(Value::Null);
|
||||
}
|
||||
parse_field_value(s.kind, raw).map_err(|e| format!("param '{label}': {e}"))
|
||||
}
|
||||
|
||||
/// Inferencia de tipo para values pasados como `params` a un
|
||||
/// morphism. Usada como fallback en [`resolve_param_value`] cuando el
|
||||
/// param declarado en `Action::Morphism.params` no aparece en los
|
||||
/// `form.fields` (módulo mal-formado).
|
||||
///
|
||||
/// Heurística simple: int → i64, float → f64, "true"/"false" → bool,
|
||||
/// resto → string.
|
||||
pub fn infer_param_value(raw: &str) -> Value {
|
||||
if raw.is_empty() {
|
||||
return Value::Null;
|
||||
}
|
||||
if let Ok(i) = raw.parse::<i64>() {
|
||||
return json!(i);
|
||||
}
|
||||
if let Ok(f) = raw.parse::<f64>() {
|
||||
return json!(f);
|
||||
}
|
||||
match raw {
|
||||
"true" => return json!(true),
|
||||
"false" => return json!(false),
|
||||
_ => {}
|
||||
}
|
||||
json!(raw)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use yahweh_meta_schema::FieldSpec;
|
||||
|
||||
fn spec(name: &str, kind: FieldKind, required: bool) -> FieldSpec {
|
||||
FieldSpec {
|
||||
name: name.into(),
|
||||
label: name.into(),
|
||||
kind,
|
||||
default: None,
|
||||
required,
|
||||
help: None,
|
||||
ref_entity: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infer_handles_basic_types() {
|
||||
assert_eq!(infer_param_value(""), Value::Null);
|
||||
assert_eq!(infer_param_value("42"), json!(42));
|
||||
assert_eq!(infer_param_value("3.14"), json!(3.14));
|
||||
assert_eq!(infer_param_value("true"), json!(true));
|
||||
assert_eq!(infer_param_value("false"), json!(false));
|
||||
assert_eq!(infer_param_value("hola"), json!("hola"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_text_passthrough() {
|
||||
let v = parse_field_value(FieldKind::Text, "hola").unwrap();
|
||||
assert_eq!(v, json!("hola"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_number_i64_or_f64() {
|
||||
assert_eq!(parse_field_value(FieldKind::Number, "42").unwrap(), json!(42));
|
||||
assert_eq!(
|
||||
parse_field_value(FieldKind::Number, "3.14").unwrap(),
|
||||
json!(3.14)
|
||||
);
|
||||
assert!(parse_field_value(FieldKind::Number, "abc").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_boolean_recognizes_variants() {
|
||||
for s in ["true", "yes", "1", "on", "y"] {
|
||||
assert_eq!(parse_field_value(FieldKind::Boolean, s).unwrap(), json!(true));
|
||||
}
|
||||
for s in ["false", "no", "0", "off", "n", ""] {
|
||||
assert_eq!(
|
||||
parse_field_value(FieldKind::Boolean, s).unwrap(),
|
||||
json!(false)
|
||||
);
|
||||
}
|
||||
assert!(parse_field_value(FieldKind::Boolean, "abc").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_entity_ref_accepts_valid_uuid() {
|
||||
let id = Uuid::new_v4();
|
||||
let v = parse_field_value(FieldKind::EntityRef, &id.to_string()).unwrap();
|
||||
assert_eq!(v, json!(id.to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_entity_ref_trims_whitespace() {
|
||||
let id = Uuid::new_v4();
|
||||
let padded = format!(" {id}\n");
|
||||
let v = parse_field_value(FieldKind::EntityRef, &padded).unwrap();
|
||||
assert_eq!(v, json!(id.to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_entity_ref_rejects_non_uuid() {
|
||||
let err = parse_field_value(FieldKind::EntityRef, "abc-123").unwrap_err();
|
||||
assert!(err.contains("'abc-123'"));
|
||||
assert!(err.contains("UUID") || err.contains("uuid"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_entity_ref_rejects_empty_string() {
|
||||
let err = parse_field_value(FieldKind::EntityRef, "").unwrap_err();
|
||||
assert!(err.contains("UUID"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_param_strict_number_parses_i64() {
|
||||
let s = spec("qty", FieldKind::Number, true);
|
||||
let v = resolve_param_value("qty", "42", Some(&s)).unwrap();
|
||||
assert_eq!(v, json!(42));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_param_strict_boolean_rejects_non_boolean() {
|
||||
let s = spec("active", FieldKind::Boolean, true);
|
||||
let err = resolve_param_value("active", "abc", Some(&s)).unwrap_err();
|
||||
assert!(err.contains("active"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_param_required_empty_rejected() {
|
||||
let s = spec("name", FieldKind::Text, true);
|
||||
let err = resolve_param_value("name", " ", Some(&s)).unwrap_err();
|
||||
assert!(err.contains("obligatorio"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_param_optional_empty_returns_null() {
|
||||
let s = spec("notes", FieldKind::Text, false);
|
||||
let v = resolve_param_value("notes", "", Some(&s)).unwrap();
|
||||
assert_eq!(v, Value::Null);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_param_no_spec_falls_back_to_infer() {
|
||||
let v = resolve_param_value("foo", "42", None).unwrap();
|
||||
assert_eq!(v, json!(42));
|
||||
let v = resolve_param_value("foo", "true", None).unwrap();
|
||||
assert_eq!(v, json!(true));
|
||||
let v = resolve_param_value("foo", "x", None).unwrap();
|
||||
assert_eq!(v, json!("x"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_param_strict_entity_ref_propagates_error() {
|
||||
let s = spec("stock_ref", FieldKind::EntityRef, true);
|
||||
let err = resolve_param_value("stock_ref", "not-a-uuid", Some(&s)).unwrap_err();
|
||||
assert!(err.contains("stock_ref"));
|
||||
assert!(err.contains("UUID"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
//! Validación cross-field de EntityRefs contra el store actual.
|
||||
//!
|
||||
//! Decoupling: en vez de un `trait Store` que ate este crate a un
|
||||
//! backend específico, tomamos un cierre `load: Fn(&str, Uuid) ->
|
||||
//! Option<Value>`. El caller (nakui-ui o cualquier otro runtime)
|
||||
//! puede pasarlo trivialmente sobre cualquier store (MemoryStore,
|
||||
//! SurrealStore, mock, ...).
|
||||
|
||||
use serde_json::Value;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::format::short_uuid;
|
||||
|
||||
/// Valida que cada UUID en `refs` apunte a un record que realmente
|
||||
/// existe en el store bajo la entity esperada. Devuelve el primer
|
||||
/// error encontrado (fail-fast).
|
||||
///
|
||||
/// `refs` es una lista de `(label, target_entity, uuid)`. El label
|
||||
/// va al error message, así que conviene que sea legible (ej:
|
||||
/// `FieldSpec.label` en lugar de `FieldSpec.name`).
|
||||
///
|
||||
/// `load` es el cierre que el caller usa para mirar el store —
|
||||
/// típicamente `|e, id| store.load(e, id)`.
|
||||
pub fn validate_entity_refs<F>(load: F, refs: &[(String, String, Uuid)]) -> Result<(), String>
|
||||
where
|
||||
F: Fn(&str, Uuid) -> Option<Value>,
|
||||
{
|
||||
for (label, target, id) in refs {
|
||||
if load(target, *id).is_none() {
|
||||
return Err(format!(
|
||||
"campo '{label}': record {} de '{target}' no existe en el store",
|
||||
short_uuid(id)
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// "Mock store" minimalista para tests: HashMap por (entity, uuid).
|
||||
fn mk_load(records: HashMap<(String, Uuid), Value>) -> impl Fn(&str, Uuid) -> Option<Value> {
|
||||
move |e, id| records.get(&(e.to_string(), id)).cloned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn passes_when_all_records_exist() {
|
||||
let stock = Uuid::new_v4();
|
||||
let caja = Uuid::new_v4();
|
||||
let mut records = HashMap::new();
|
||||
records.insert(("Stock".into(), stock), json!({"sku_id": "abc"}));
|
||||
records.insert(("Caja".into(), caja), json!({"name": "Principal"}));
|
||||
let load = mk_load(records);
|
||||
|
||||
let refs = vec![
|
||||
("Stock".into(), "Stock".into(), stock),
|
||||
("Caja".into(), "Caja".into(), caja),
|
||||
];
|
||||
assert!(validate_entity_refs(load, &refs).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fails_on_first_missing() {
|
||||
let stock = Uuid::new_v4();
|
||||
let mut records = HashMap::new();
|
||||
records.insert(("Stock".into(), stock), json!({"sku_id": "abc"}));
|
||||
let load = mk_load(records);
|
||||
|
||||
let missing_caja = Uuid::new_v4();
|
||||
let refs = vec![
|
||||
("Stock".into(), "Stock".into(), stock),
|
||||
("Caja".into(), "Caja".into(), missing_caja),
|
||||
];
|
||||
let err = validate_entity_refs(load, &refs).unwrap_err();
|
||||
assert!(err.contains("Caja"));
|
||||
assert!(err.contains(&short_uuid(&missing_caja)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn uses_label_not_entity_in_msg() {
|
||||
let load = |_: &str, _: Uuid| -> Option<Value> { None };
|
||||
let id = Uuid::new_v4();
|
||||
let refs = vec![("Stock origen".into(), "Stock".into(), id)];
|
||||
let err = validate_entity_refs(load, &refs).unwrap_err();
|
||||
assert!(err.contains("Stock origen"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_list_is_ok() {
|
||||
let load = |_: &str, _: Uuid| -> Option<Value> { None };
|
||||
assert!(validate_entity_refs(load, &[]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distinguishes_target_from_other_entities() {
|
||||
let id = Uuid::new_v4();
|
||||
let mut records = HashMap::new();
|
||||
// Mismo UUID bajo Customer pero NO bajo Stock.
|
||||
records.insert(("Customer".into(), id), json!({"name": "Acme"}));
|
||||
let load = mk_load(records);
|
||||
let refs = vec![("Stock".into(), "Stock".into(), id)];
|
||||
assert!(validate_entity_refs(load, &refs).is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user