Skip to main content

coerce_to_schema

Function coerce_to_schema 

Source
pub fn coerce_to_schema(doc: &mut Document, schema: &Value) -> Result<usize>
Expand description

Apply schema-driven type coercions to doc losslessly: only the bytes of the coerced scalars are rewritten; comments, indentation, and sibling entries survive byte-faithfully.

Returns the number of coercions applied. 0 means the input already matches the schema (or no coercion is possible — e.g. port: "abc" against type: integer).

§Errors

  • Schema does not parse as a valid JSON Schema 2020-12 document.
  • The CST Document::set operation rejects the spliced fragment (would only happen on adversarial schemas that request a coercion to a non-scalar type — guarded against here, but the error is surfaced if it slips through).