Exdantic.JsonSchema (exdantic v0.0.2)

View Source

A module for converting Exdantic schema definitions into JSON Schema format. Handles field types, metadata, references, and definitions generation.

Summary

Functions

Extracts computed field information from a JSON schema.

Converts an Exdantic schema module to a JSON Schema representation.

Checks if a JSON schema contains computed fields.

Removes computed fields from a JSON schema.

Types

json_schema()

@type json_schema() :: %{required(String.t()) => term()}

Functions

extract_computed_field_info(arg1)

@spec extract_computed_field_info(json_schema()) :: [map()]

Extracts computed field information from a JSON schema.

This function can parse a JSON schema generated by Exdantic and extract information about computed fields from the x-computed-field metadata.

Parameters

  • json_schema - The JSON schema to analyze

Returns

  • List of computed field information maps

Examples

iex> schema = Exdantic.JsonSchema.from_schema(UserSchema)
iex> Exdantic.JsonSchema.extract_computed_field_info(schema)
[
  %{
    name: "full_name",
    type: %{"type" => "string"},
    function: "UserSchema.generate_full_name/1",
    readonly: true
  }
]

from_schema(schema)

@spec from_schema(module()) :: json_schema()

Converts an Exdantic schema module to a JSON Schema representation.

Parameters

  • schema - The schema module to convert

Returns

  • A map representing the JSON Schema

Examples

iex> defmodule TestSchema do
...>   use Exdantic
...>   schema do
...>     field :name, :string
...>   end
...> end
iex> Exdantic.JsonSchema.from_schema(TestSchema)
%{"type" => "object", "properties" => %{"name" => %{"type" => "string"}}}

has_computed_fields?(json_schema)

@spec has_computed_fields?(json_schema()) :: boolean()

Checks if a JSON schema contains computed fields.

Parameters

  • json_schema - The JSON schema to check

Returns

  • true if the schema contains computed fields, false otherwise

Examples

iex> schema = Exdantic.JsonSchema.from_schema(UserSchema)
iex> Exdantic.JsonSchema.has_computed_fields?(schema)
true

remove_computed_fields(json_schema)

@spec remove_computed_fields(json_schema()) :: json_schema()

Removes computed fields from a JSON schema.

This can be useful when you want to generate a schema for input validation that excludes computed fields, since computed fields are output-only.

Parameters

  • json_schema - The JSON schema to process

Returns

  • JSON schema with computed fields removed

Examples

iex> schema = Exdantic.JsonSchema.from_schema(UserSchema)
iex> input_schema = Exdantic.JsonSchema.remove_computed_fields(schema)
# input_schema will not contain computed fields