pub struct Schema<C> { /* private fields */ }Expand description
A set of field rules. Matched against a row’s fig path to find what governs it.
Implementations§
Source§impl<C: Validate> Schema<C>
impl<C: Validate> Schema<C>
Sourcepub fn check(&self, document: &Value) -> Vec<Verdict>
pub fn check(&self, document: &Value) -> Vec<Verdict>
Check a whole parsed document against this schema: every node, against
the rule Schema::rule_for names for it, twice — its shape against
the rule’s ty, then its value against the rule’s
constraint. What comes back is only what is wrong or unchecked, in
post-order, so a clean document yields nothing.
Three rules of the walk are choices rather than consequences, and each could have gone the other way:
- A node no rule governs is not a verdict. A schema governs what it
names. A check that fired on a document’s own
title/author— keys the schema never mentions — is one people stop running. - The same issue about the same value is reported once, at the
deepest path.
validate_enumvalidates a sequence element-wise, so a rule ataudienceand a rule ataudience[]— an embedder emits both, so a field is governed whether written as a scalar or a list — would otherwise report one misspelling twice. The walk is post-order, and a container’s issue is dropped when a descendant already reported an equal one. - An empty document is valid. The model has no notion of a
required field: a
FieldRulesays what a value must be if there is one, and nothing says there must be one. So a check never reports a missing field, and a document with nothing in it has nothing wrong with it.
One limit besides: a mapping entry whose key is not text has no Seg
to address it, so no rule can govern it and it is not walked.
use fig::Value;
use fig_schema::{FieldRule, FieldType, PathPat, Schema, Term, Validate, Validation, validate_enum};
struct Vocabulary(Vec<Term>);
impl Validate for Vocabulary {
fn validate(&self, value: &Value) -> Validation {
validate_enum(&self.0, true, value)
}
}
let schema = Schema::new(vec![
FieldRule::new(PathPat::each_item_of("audience"))
.ty(FieldType::Str)
.constraint(Vocabulary(vec![Term::value("public"), Term::value("family")])),
FieldRule::new(PathPat::key("count")).ty(FieldType::Int),
]);
let document = fig::Document::parse(
b"audience: [public, famly]\ncount: three\ntitle: untouched\n",
fig_yaml::format().unwrap(),
).unwrap();
let verdicts = schema.check(&document.to_value().unwrap());
let report: Vec<String> = verdicts.iter().map(|v| format!("{}: {v}", v.at())).collect();
assert_eq!(report, [
"audience[1]: “famly” is not a known value — did you mean “family”?",
"count: expected int, found str",
]);
assert!(verdicts.iter().all(|v| v.is_error()));Source§impl<C> Schema<C>
impl<C> Schema<C>
Sourcepub fn into_rules(self) -> Vec<FieldRule<C>>
pub fn into_rules(self) -> Vec<FieldRule<C>>
The rules, owned — for concatenating schemas, or for mapping every
constraint into another type with FieldRule::map_constraint.
Sourcepub fn map_constraints<D>(self, f: impl FnMut(C) -> D) -> Schema<D>
pub fn map_constraints<D>(self, f: impl FnMut(C) -> D) -> Schema<D>
Every rule’s constraint mapped into another type; see
FieldRule::map_constraint.
Sourcepub fn rule_for(&self, path: &[Seg]) -> Option<&FieldRule<C>>
pub fn rule_for(&self, path: &[Seg]) -> Option<&FieldRule<C>>
The first rule whose pattern matches path, if any. Declaration order is
precedence, so a more specific rule should be listed before a broader one.
Sourcepub fn rules_for<'s, 'p: 's>(
&'s self,
path: &'p [Seg],
) -> impl Iterator<Item = &'s FieldRule<C>> + 's
pub fn rules_for<'s, 'p: 's>( &'s self, path: &'p [Seg], ) -> impl Iterator<Item = &'s FieldRule<C>> + 's
Every rule whose pattern matches path, in precedence order — the
first is what Schema::rule_for returns, and the rest are the rules
it shadows. That is what makes precedence visible: a schema composed
from several documents decides it by include order, and this is how a
tool shows a person which later rule a path would otherwise have hit.