jscheam/schema
Types
Additional properties configuration for object types
pub type AdditionalProperties {
AllowAny
AllowExplicit
Disallow
Schema(Type)
}
Constructors
-
AllowAnyAllow any additional properties (JSON Schema Draft 7 default behavior) This is the default and will omit the additionalProperties field from the schema
-
AllowExplicitExplicitly allow any additional properties (outputs “additionalProperties”: true)
-
DisallowDisallow any additional properties
-
Schema(Type)Additional properties must conform to the specified schema
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
-
Property( name: String, property_type: Type, is_required: Bool, description: option.Option(String), constraints: List(Constraint), )
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 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 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