jscheam/schema

Types

Additional properties configuration for object types

pub type AdditionalProperties {
  AllowAny
  AllowExplicit
  Disallow
  Schema(Type)
}

Constructors

  • AllowAny

    Allow any additional properties (JSON Schema Draft 7 default behavior) This is the default and will omit the additionalProperties field from the schema

  • AllowExplicit

    Explicitly allow any additional properties (outputs “additionalProperties”: true)

  • Disallow

    Disallow any additional properties

  • Schema(Type)

    Additional properties must conform to the specified schema

Constraints that can be applied to properties

pub type Constraint {
  Enum(values: List(json.Json))
  Pattern(regex: String)
}

Constructors

  • Enum(values: List(json.Json))

    Restrict values to a fixed set of values (can be any JSON value)

  • Pattern(regex: String)

    Pattern constraint using regex

A property in a object type Represents a field in an object with a name, type, and optional constraints

pub type Property {
  Property(
    name: String,
    property_type: Type,
    is_required: Bool,
    description: option.Option(String),
    constraints: List(Constraint),
  )
}

Constructors

A type definition for JSON Schema

pub type Type {
  Integer
  String
  Boolean
  Float
  Null
  Object(
    properties: List(Property),
    additional_properties: AdditionalProperties,
  )
  Array(Type)
  Union(List(Type))
}

Constructors

  • Integer
  • String
  • Boolean
  • Float
  • Null
  • Object(
      properties: List(Property),
      additional_properties: AdditionalProperties,
    )
  • Array(Type)
  • Union(List(Type))

    Union type for multiple allowed types (e.g., [“string”, “null”])

Values

pub fn allow_additional_props(object_type: Type) -> Type

Update an object type to allow any additional properties Explicitly allows any additional properties (outputs “additionalProperties”: true)

pub fn array(item_type: Type) -> Type

Creates an array type with the specified item type

pub fn boolean() -> Type

Creates a boolean type for JSON Schema

pub fn constrain_additional_props(
  object_type: Type,
  schema: Type,
) -> Type

Update an object type to constrain additional properties to a specific schema Example: object([prop(“name”, string())]) |> constrain_additional_props(string()) This will set “additionalProperties” to the specified schema type

pub fn description(property: Property, desc: String) -> Property

Adds a description to a property for documentation purposes Example: prop(“name”, string()) |> description(“The name of the person”)

pub fn disallow_additional_props(object_type: Type) -> Type

Update an object type to disallow additional properties Disallows additional properties (outputs “additionalProperties”: false)

pub fn enum(
  property: Property,
  values: List(json.Json),
) -> Property

Adds an enum constraint to a property that restricts values to a fixed set Example: prop(“color”, string()) |> enum(enum_strings([“red”, “green”, “blue”]))

pub fn float() -> Type

Creates a float/number type for JSON Schema

pub fn integer() -> Type

Creates an integer/number type for JSON Schema

pub fn null() -> Type

Creates a null type for JSON Schema

pub fn object(properties: List(Property)) -> Type

Creates an object type with the specified properties By default allows any additional properties (JSON Schema default behavior - omits the field)

pub fn optional(property: Property) -> Property

Makes a property optional (not required in the schema) Example: object([prop(“name”, string()) |> optional()])

pub fn pattern(property: Property, regex: String) -> Property

Adds a pattern constraint to a property that restricts values to match a regex pattern Example: prop(“phone”, string()) |> pattern(“^(\([0-9]{3}\))?[0-9]{3}-[0-9]{4}$”)

pub fn prop(name: String, property_type: Type) -> Property

Creates a property with the specified name and type Properties are required by default

pub fn string() -> Type

Creates a string type for JSON Schema

pub fn to_json(object_type: Type) -> json.Json

Converts a Type to a JSON Schema document This is the main function to generate JSON Schema from your type definitions Example: object([prop(“name”, string()), prop(“age”, integer())]) |> to_json()

pub fn union(types: List(Type)) -> Type

Creates a union type that accepts multiple types (e.g., string or null) Example: union([string(), null()]) creates a schema that accepts both strings and null values

Search Document