fix(anatomy): R5.3 shapes refused as Zod did (struct as array, enum as map), reads bounded to regular files, --check never follows links, plain names in --out
This commit is contained in:
1 parent
314cf04e6a
commit
776c7095b3
9 files changed
+258
-25
No files matched your search
@@ -16,6 +16,7 @@ use serde_json::Value;
|
||||
use super::ops::{apply_op, assert_declared};
|
||||
use super::records::{ChangeRecord, CurationResult, Recorder, strip_marks, touch_order};
|
||||
use super::schema::{Change, ChangeSummary, Op, SetOp};
|
||||
use super::shape::assert_same_shape;
|
||||
use super::targets::{
|
||||
Path, Step, TargetKind, all_findings, at, fail, item_at_mut, list_at, top_collection_path,
|
||||
};
|
||||
@@ -159,6 +160,7 @@ fn describe(issues: &[SheetIssue]) -> String {
|
||||
pub fn curated_issues(doc: &Value) -> Result<Vec<SheetIssue>, PipelineError> {
|
||||
let parsed: AnatomicalSheetDocument = serde_json::from_value(doc.clone())
|
||||
.map_err(|error| fail(format!("curated document is not shape-valid: {error}")))?;
|
||||
assert_same_shape("curated document is not shape-valid", doc, &parsed)?;
|
||||
let issues = validate_sheet_document(&parsed);
|
||||
let errors: Vec<SheetIssue> = issues
|
||||
.iter()
|
||||
|
||||
@@ -13,6 +13,7 @@ pub mod records;
|
||||
pub mod rename;
|
||||
pub mod schema;
|
||||
pub mod seed;
|
||||
pub mod shape;
|
||||
pub mod stats;
|
||||
pub mod targets;
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use super::shape::assert_same_shape;
|
||||
use crate::curation::PipelineError;
|
||||
use crate::domain::js;
|
||||
use crate::domain::sheet::Reference;
|
||||
@@ -246,7 +247,7 @@ pub struct ChangeSummary {
|
||||
}
|
||||
|
||||
/// One authored change: `ChangeSchema` (strict, `priority` defaulted, `ops` non-empty).
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Change {
|
||||
#[serde(deserialize_with = "change_id")]
|
||||
@@ -258,7 +259,11 @@ pub struct Change {
|
||||
pub rationale: String,
|
||||
#[serde(deserialize_with = "non_empty_strings")]
|
||||
pub reference_ids: Vec<String>,
|
||||
#[serde(default, deserialize_with = "optional_non_empty")]
|
||||
#[serde(
|
||||
default,
|
||||
deserialize_with = "optional_non_empty",
|
||||
skip_serializing_if = "Option::is_none"
|
||||
)]
|
||||
pub question: Option<String>,
|
||||
#[serde(default = "default_priority")]
|
||||
pub priority: ChangePriority,
|
||||
@@ -286,13 +291,13 @@ impl Change {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Changeset {
|
||||
pub changes: Vec<Change>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ReferencesFile {
|
||||
references: Vec<Reference>,
|
||||
@@ -407,7 +412,8 @@ pub fn parse_changeset(text: &str) -> Result<Changeset, PipelineError> {
|
||||
return Err(refuse("changes.json", format!("reserved key {key:?}")));
|
||||
}
|
||||
let changeset: Changeset =
|
||||
serde_json::from_value(value).map_err(|e| refuse("changes.json", e))?;
|
||||
serde_json::from_value(value.clone()).map_err(|e| refuse("changes.json", e))?;
|
||||
assert_same_shape("changes.json", &value, &changeset)?;
|
||||
for change in &changeset.changes {
|
||||
if change.level == ChangeLevel::C && change.question.is_none() {
|
||||
return Err(refuse(
|
||||
@@ -427,7 +433,8 @@ pub fn parse_changeset(text: &str) -> Result<Changeset, PipelineError> {
|
||||
pub fn parse_references(text: &str) -> Result<Vec<Reference>, PipelineError> {
|
||||
let value = json_parse("references.json", text)?;
|
||||
let file: ReferencesFile =
|
||||
serde_json::from_value(value).map_err(|e| refuse("references.json", e))?;
|
||||
serde_json::from_value(value.clone()).map_err(|e| refuse("references.json", e))?;
|
||||
assert_same_shape("references.json", &value, &file)?;
|
||||
let ids = file
|
||||
.references
|
||||
.iter()
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
//! The JSON shapes Zod refused and serde's derives accept. serde reads a struct from a
|
||||
//! positional array (`"title": ["Titre", "Title"]`) and a unit enum variant from a one-key map
|
||||
//! (`"level": {"A": null}`); Zod refused both (`invalid_type`). The authored files and the
|
||||
//! curated document are therefore compared, by shape, with the typed value serialised back:
|
||||
//! an object must stay an object, an array an array of the same length, a scalar a scalar of
|
||||
//! the same kind. Keys the typed value adds (defaults filled in) are allowed.
|
||||
//!
|
||||
//! The R2 contracts carry the same derives; their readers are outside R5.3 (owner's go).
|
||||
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
|
||||
use super::targets::fail;
|
||||
use crate::curation::PipelineError;
|
||||
|
||||
fn kind(value: &Value) -> &'static str {
|
||||
match value {
|
||||
Value::Null => "null",
|
||||
Value::Bool(_) => "a boolean",
|
||||
Value::Number(_) => "a number",
|
||||
Value::String(_) => "a string",
|
||||
Value::Array(_) => "an array",
|
||||
Value::Object(_) => "an object",
|
||||
}
|
||||
}
|
||||
|
||||
fn mismatch(path: &str, typed: &Value, written: &Value) -> String {
|
||||
let at = if path.is_empty() { "the root" } else { path };
|
||||
format!(
|
||||
"expected {} at {at}, received {}",
|
||||
kind(typed),
|
||||
kind(written)
|
||||
)
|
||||
}
|
||||
|
||||
/// The first place where `written` (as parsed) and `typed` (read and serialised back) differ
|
||||
/// in shape.
|
||||
fn first_mismatch(written: &Value, typed: &Value, path: &str) -> Option<String> {
|
||||
match (written, typed) {
|
||||
(Value::Object(w), Value::Object(t)) => w.iter().find_map(|(key, value)| {
|
||||
let here = format!("{path}.{key}");
|
||||
match t.get(key) {
|
||||
Some(typed) => first_mismatch(value, typed, &here),
|
||||
None => Some(format!("unexpected key at {here}")),
|
||||
}
|
||||
}),
|
||||
(Value::Array(w), Value::Array(t)) if w.len() == t.len() => w
|
||||
.iter()
|
||||
.zip(t)
|
||||
.enumerate()
|
||||
.find_map(|(index, (w, t))| first_mismatch(w, t, &format!("{path}.{index}"))),
|
||||
_ if std::mem::discriminant(written) == std::mem::discriminant(typed) => None,
|
||||
_ => Some(mismatch(path, typed, written)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Refuses `written` when its typed reading `typed` does not keep its shape.
|
||||
pub fn assert_same_shape<T: Serialize>(
|
||||
what: &str,
|
||||
written: &Value,
|
||||
typed: &T,
|
||||
) -> Result<(), PipelineError> {
|
||||
let typed = serde_json::to_value(typed).map_err(|error| fail(format!("{what}: {error}")))?;
|
||||
match first_mismatch(written, &typed, "") {
|
||||
None => Ok(()),
|
||||
Some(why) => Err(fail(format!("{what}: {why}"))),
|
||||
}
|
||||
}
|
||||
@@ -190,9 +190,11 @@ pub fn at_mut<'a>(doc: &'a mut Value, path: &[Step]) -> Result<&'a mut Value, Pi
|
||||
/// `listAt`: the array at a path, or "document has no list".
|
||||
pub fn list_at<'a>(doc: &'a Value, path: &[Step]) -> Result<&'a Vec<Value>, PipelineError> {
|
||||
let last = path.last().map(describe).unwrap_or_default();
|
||||
at(doc, path)?
|
||||
let no_list = || fail(format!("document has no list \"{last}\""));
|
||||
at(doc, path)
|
||||
.map_err(|_| no_list())?
|
||||
.as_array()
|
||||
.ok_or_else(|| fail(format!("document has no list \"{last}\"")))
|
||||
.ok_or_else(no_list)
|
||||
}
|
||||
|
||||
pub fn list_at_mut<'a>(
|
||||
@@ -200,9 +202,11 @@ pub fn list_at_mut<'a>(
|
||||
path: &[Step],
|
||||
) -> Result<&'a mut Vec<Value>, PipelineError> {
|
||||
let last = path.last().map(describe).unwrap_or_default();
|
||||
at_mut(doc, path)?
|
||||
let no_list = || fail(format!("document has no list \"{last}\""));
|
||||
at_mut(doc, path)
|
||||
.map_err(|_| no_list())?
|
||||
.as_array_mut()
|
||||
.ok_or_else(|| fail(format!("document has no list \"{last}\"")))
|
||||
.ok_or_else(no_list)
|
||||
}
|
||||
|
||||
pub fn item_at<'a>(doc: &'a Value, path: &[Step]) -> Result<&'a Item, PipelineError> {
|
||||
|
||||
Reference in new issue
Block a user