Learn R Programming

schemate (version 0.1.0)

schema_modify_where: Modify schema nodes selected by a predicate

Description

schema_modify_where() modifies every schema node matched by where. schema_replace_where() is a convenience wrapper that replaces all matched nodes with the same schema fragment.

Usage

schema_modify_where(x, where, fn, defs = TRUE, missing = "ignore")

schema_replace_where(x, where, value, defs = TRUE, missing = "ignore")

Value

A modified SchemaDoc.

Arguments

x

A schema document or raw schema DSL list.

where

Predicate function with signature function(path, node).

fn

Function with signature function(path, node) returning a schema fragment or SchemaNode.

defs

Whether to include root $defs entries.

missing

Missing-match behavior. Use "error" to raise an error when where matches no paths or "ignore" to leave the schema unchanged.

value

Replacement schema fragment or SchemaNode.

Details

Batch edits operate on logical paths. Editing paths inside a grouped schema field may split the group into per-field bindings. If where matches both a node and one of its descendants in the same call, the edit errors and asks you to narrow the selector.

Examples

Run this code
schema <- schema_compact(schema_infer(list(
    issued = list(`date-parts` = list(list(2024L))),
    created = list(`date-parts` = list(list(2024L)))
), arrays = "rest"))
schema <- schema_add_def(schema, "year", schema_check("int", lower = 0))
schema

schema_find(schema, schema_where_path("(^|\\$)`date-parts`\\$rest$"))

schema <- schema_replace_where(
    schema,
    schema_where_path("(^|\\$)`date-parts`\\$rest$"),
    schema_ref("year")
)
schema

Run the code above in your browser using DataLab