Class ObjectSchema

java.lang.Object
io.github.glaforge.gemini.schema.Schema
io.github.glaforge.gemini.schema.ObjectSchema

public class ObjectSchema extends Schema
Schema for Object types.

Example usage:


 obj()
     .str("name")
     .integer("age");
 

Note on convenience methods: Methods like str(name), obj(name), etc., add a property to the current object and return the current object schema to allow chaining siblings.

For example:


 obj()
     .obj("first")   // Adds "first" property
     .obj("second"); // Adds "second" property (sibling of "first")
 
Creates: { "first": {}, "second": {} }

To create nested objects, use prop(name, schema):


 obj()
     .prop("parent", obj()
         .str("child"));
 
  • Constructor Details

    • ObjectSchema

      public ObjectSchema()
      Initializes a new object schema.
  • Method Details

    • prop

      public ObjectSchema prop(String name, Schema schema)
      Add a property to this object schema.
      Parameters:
      name - The name of the property.
      schema - The schema for the property.
      Returns:
      The ObjectSchema instance.
    • req

      public ObjectSchema req(String... names)
      Specify required properties.
      Parameters:
      names - The names of the required properties.
      Returns:
      The ObjectSchema instance.
    • reqProp

      public ObjectSchema reqProp(String name, Schema schema)
      Helper to add a required property in one go.
      Parameters:
      name - The name of the property.
      schema - The schema for the property.
      Returns:
      The ObjectSchema instance.
    • bool

      public ObjectSchema bool(String name)
      Add a boolean property.
      Parameters:
      name - The name of the property.
      Returns:
      The ObjectSchema instance.
    • str

      public ObjectSchema str(String name)
      Add a string property.
      Parameters:
      name - The name of the property.
      Returns:
      The ObjectSchema instance.
    • str

      public ObjectSchema str(String name, StringSchema.Format format)
      Add a string property with a specific format.
      Parameters:
      name - The name of the property.
      format - The format of the string.
      Returns:
      The ObjectSchema instance.
    • str

      public ObjectSchema str(String name, List<String> enumValues)
      Add a string property with allowed enum values.
      Parameters:
      name - The name of the property.
      enumValues - The allowed values.
      Returns:
      The ObjectSchema instance.
    • num

      public ObjectSchema num(String name)
      Add a number property.
      Parameters:
      name - The name of the property.
      Returns:
      The ObjectSchema instance.
    • num

      public ObjectSchema num(String name, double min, double max)
      Add a number property with min and max constraints.
      Parameters:
      name - The name of the property.
      min - The minimum value.
      max - The maximum value.
      Returns:
      The ObjectSchema instance.
    • num

      public ObjectSchema num(String name, List<Double> enumValues)
      Add a number property with allowed enum values.
      Parameters:
      name - The name of the property.
      enumValues - The allowed values.
      Returns:
      The ObjectSchema instance.
    • integer

      public ObjectSchema integer(String name)
      Add an integer property.
      Parameters:
      name - The name of the property.
      Returns:
      The ObjectSchema instance.
    • integer

      public ObjectSchema integer(String name, long min, long max)
      Add an integer property with min and max constraints.
      Parameters:
      name - The name of the property.
      min - The minimum value.
      max - The maximum value.
      Returns:
      The ObjectSchema instance.
    • integer

      public ObjectSchema integer(String name, List<Long> enumValues)
      Add an integer property with allowed enum values.
      Parameters:
      name - The name of the property.
      enumValues - The allowed values.
      Returns:
      The ObjectSchema instance.
    • arr

      public ObjectSchema arr(String name)
      Add an array property.
      Parameters:
      name - The name of the property.
      Returns:
      The ObjectSchema instance.
    • arr

      public ObjectSchema arr(String name, Schema itemSchema)
      Add an array property with specified item schema.
      Parameters:
      name - The name of the property.
      itemSchema - The schema for the items in the array.
      Returns:
      The ObjectSchema instance.
    • obj

      public ObjectSchema obj(String name)
      Add an object property.
      Parameters:
      name - The name of the property.
      Returns:
      The ObjectSchema instance.
    • addProps

      public ObjectSchema addProps(boolean allowed)
      Allow or disallow additional properties.
      Parameters:
      allowed - Whether additional properties are allowed.
      Returns:
      The ObjectSchema instance.
    • addProps

      public ObjectSchema addProps(Schema schema)
      Define the schema for additional properties.
      Parameters:
      schema - The schema for additional properties.
      Returns:
      The ObjectSchema instance.
    • toMap

      public Map<String,Object> toMap()
      Description copied from class: Schema
      Convert this Schema object into a Map suitable for JSON serialization.
      Overrides:
      toMap in class Schema
      Returns:
      A Map representing the JSON schema.