Skip to contents

Define a schema field

Usage

field(
  name,
  description = name,
  type = c("string", "number", "integer", "boolean", "array"),
  enum = NULL,
  items = NULL,
  required = TRUE
)

Arguments

name

Optional Character: The name of the field.

description

Optional Character: A brief description of the field.

type

Character {"string", "number", "integer", "boolean", "array"}: The field type. A field cannot be an object, because it carries no properties; for an array of objects, set type = "array" and items = schema(...).

enum

Optional Character: Permitted values for this field. Only for type "string", "number" or "integer". Backends that support constrained decoding (e.g. Ollama) make any other value impossible rather than merely detectable.

items

Character, Field or Schema: What an array field contains. Required for type = "array" and forbidden otherwise. Give a type name ("string", "number", "integer", "boolean"), a field where the elements need their own description or enum, or a schema for an array of objects.

required

Logical: Whether the field is required.

Value

Field object

Author

EDG

Examples

# `type` defaults to "string", `required` defaults to TRUE
field("lab_name", "Name of the lab test")
#> lab_name: 
#>                  type: string
#>           description: Name of the lab test
#>                  enum: NULL
#>                 items: NULL
#>              required: TRUE
#>  
field("normal_range_low", "Lower bound of normal range", type = "number")
#> normal_range_low: 
#>                          type: number
#>                   description: Lower bound of normal range
#>                          enum: NULL
#>                         items: NULL
#>                      required: TRUE
#>  
field("flag", "Whether the result is out of range", enum = c("low", "normal", "high"))
#> flag: 
#>              type: string
#>       description: Whether the result is out of range
#>              enum: low, normal, high
#>             items: NULL
#>          required: TRUE
#>  

# An array of strings: one element per item, so nothing has to be delimited
# inside a single string and later split back apart.
field("questions", "Each question, quoted as written", type = "array", items = "string")
#> questions: 
#>                   type: array
#>            description: Each question, quoted as written
#>                   enum: NULL
#>                  items: 
#>                         type: string
#>               required: TRUE
#>  

# An array whose elements carry their own description
field(
  "codes", "ICD-10 codes found",
  type = "array",
  items = field("code", "One ICD-10 code, e.g. \"E11.9\"")
)
#> codes: 
#>               type: array
#>        description: ICD-10 codes found
#>               enum: NULL
#>              items: 
#>                            type: string
#>                     description: One ICD-10 code, e.g. "E11.9"
#>           required: TRUE
#>  

# An array of objects
field(
  "results", "One row per lab result",
  type = "array",
  items = schema(
    "LabResult",
    field("name", "Test name"),
    field("value", "Result value", type = "number")
  )
)
#> results: 
#>                 type: array
#>          description: One row per lab result
#>                 enum: NULL
#>                items: 
#>                             type: object
#>                       properties: 
#>                                    name: 
#>                                                 type: string
#>                                          description: Test name
#>                                   value: 
#>                                                 type: number
#>                                          description: Result value
#>                         required: object of class 'AsIs'
#>             required: TRUE
#>