FhirEx.Validation (fhir_ex v0.2.0)

Copy Markdown View Source

Validation for FHIR R5 data types and resources.

Returns {:ok, struct} when valid, or {:validation_error, [%Error{}]} with a list of structured errors carrying a path (JSON-pointer style) and message.

Usage

alias FhirEx.Validation
alias FhirEx.Validation.Error

# Validate a data type
Validation.validate(%Coding{system: "http://loinc.org", code: "718-7"})
#=> {:ok, %Coding{...}}

Validation.validate(%Coding{code: "has spaces here"})
#=> {:validation_error, [%Error{path: ["code"], message: "must not contain whitespace..."}]}

# Validate a resource
Validation.validate(%ServiceRequest{status: "active", intent: "order", subject: %Reference{...}})
#=> {:ok, %ServiceRequest{...}}

# Raise on failure
Validation.validate!(%Patient{gender: "unknown_gender"})
#=> raises ArgumentError

# Format errors for display
{:validation_error, errors} = Validation.validate(invalid_struct)
IO.puts(Error.format_all(errors))

Error paths

Errors include a JSON-pointer style path list so you can pinpoint exactly which field failed:

%Error{path: ["name", "0", "use"], message: "must be one of: usual, official..."}

This corresponds to the field name[0].use in FHIR JSON.

Summary

Functions

Validates a FHIR data type or resource struct. Returns {:ok, struct} or {:validation_error, [%Error{}]}.

Like validate/1 but raises ArgumentError on failure.

Types

result(t)

@type result(t) :: {:ok, t} | {:validation_error, [FhirEx.Validation.Error.t()]}

Functions

validate(s)

@spec validate(struct()) :: result(struct())

Validates a FHIR data type or resource struct. Returns {:ok, struct} or {:validation_error, [%Error{}]}.

validate!(struct)

@spec validate!(struct()) :: struct()

Like validate/1 but raises ArgumentError on failure.