AshOaskit.TypeMapper (AshOasKit v0.2.1)

View Source

Maps Ash types to JSON Schema types for OpenAPI 3.0 and 3.1.

This module handles the conversion of Ash resource attributes to their corresponding JSON Schema representations, respecting the differences between OpenAPI versions.

Version Differences

  • OpenAPI 3.0: Uses nullable: true for nullable fields. $ref schemas are wrapped in allOf first, because 3.0 ignores sibling keys next to $ref.
  • OpenAPI 3.1: Uses type arrays like ["string", "null"]; $ref schemas are wrapped in oneOf with a null type.

Supported Types

Ash TypeJSON Schema TypeFormat
:stringstring-
:ci_stringstring-
:integerinteger-
:floatnumberfloat
:decimalnumberdouble
:booleanboolean-
:datestringdate
:timestringtime
:time_usecstringtime
:datetimestringdate-time
:utc_datetimestringdate-time
:utc_datetime_usecstringdate-time
:naive_datetimestringdate-time
:durationstringduration
:uuidstringuuid
:uuid_v7stringuuid
:binarystringbinary
:url_encoded_binarystringbyte
:mapobject-
:keywordobject-
:tupleobject-
:atomstring-
:modulestring-
:term(empty schema)-
:function(empty schema)-
:vectorarray of number-
{:array, type}arrayitems: nested type

Advanced Types

Ash TypeJSON SchemaNotes
Ash.Type.UnionanyOfWith optional discriminator
Ash.Type.StructobjectWith constrained properties
Ash.Type.Filestring (byte)Base64 encoded content
Ash.Type.DurationNamestringEnum from the type's values/0
Ash.Type.Enum implementorsstringEnum from the type's values/0
Ash.TypedStruct modulesobjectTyped properties and required list from the field definitions
Ash.Type.NewType wrappers(subtype schema)Resolved via subtype_of/0
Custom typesCalls json_schema/1If defined on type

Supported Constraints

Ash ConstraintJSON Schema Property
:min_lengthminLength
:max_lengthmaxLength
:minminimum
:maxmaximum
:match (Regex)pattern
:one_ofenum

Additional Schema Properties

  • description - Copied from attribute description
  • default - Copied from attribute default (non-function values only)

Summary

Functions

Convert an Ash attribute to a JSON Schema for OpenAPI 3.0.

Convert an Ash attribute to a JSON Schema for OpenAPI 3.1.

Functions

to_json_schema_30(attr)

@spec to_json_schema_30(map()) :: map()

Convert an Ash attribute to a JSON Schema for OpenAPI 3.0.

In OpenAPI 3.0, nullable is represented with a boolean flag: {"type": "string", "nullable": true}

Examples

iex> attr = %{type: :string, allow_nil?: false}
...> AshOaskit.TypeMapper.to_json_schema_30(attr)
%{"type" => "string"}

iex> attr = %{type: :string, allow_nil?: true}
...> AshOaskit.TypeMapper.to_json_schema_30(attr)
%{"type" => "string", "nullable" => true}

to_json_schema_31(attr)

@spec to_json_schema_31(map()) :: map()

Convert an Ash attribute to a JSON Schema for OpenAPI 3.1.

In OpenAPI 3.1, nullable is represented as a type array: {"type": ["string", "null"]} instead of {"type": "string", "nullable": true}

Examples

iex> attr = %{type: :string, allow_nil?: false}
...> AshOaskit.TypeMapper.to_json_schema_31(attr)
%{"type" => "string"}

iex> attr = %{type: :string, allow_nil?: true}
...> AshOaskit.TypeMapper.to_json_schema_31(attr)
%{"type" => ["string", "null"]}

iex> attr = %{type: :uuid, allow_nil?: false}
...> AshOaskit.TypeMapper.to_json_schema_31(attr)
%{"type" => "string", "format" => "uuid"}