Skip to content

Schema

A schema for a BSON document.

A schema specifies which fields are allowed, their type, and constraints on their values.

Creating an instance of a schema is much more convenient using the facilities provided in the :dsl module.

To create a schema that accepts any value, use Schema.acceptAll.

External resources

Types

AdditionalProperties

class AdditionalProperties(val schema: Schema<*>?, factory: BsonFactory) : Schema.SimpleConstraint<Any> 

Specifies the behavior of Properties and PatternProperties.

AllOf

class AllOf<T>(val schemas: List<Schema<T>>, factory: BsonFactory) : Schema.SimpleConstraint<T> 

The value must confirm to all of the given schemas.

AnyOf

class AnyOf<T>(val schemas: List<Schema<T>>, factory: BsonFactory) : Schema.SimpleConstraint<T> 

The value must confirm to at least one of the given schemas.

ArrayItems

This property must have items following the specified configuration.

Companion

object Companion

ComplexConstraint

abstract class ComplexConstraint<in T>(factory: BsonFactory) : Schema.Constraint<T> 

Helper to create instances of Constraint that handle multiple constraints.

Constraint

sealed interface Constraint<in T> : BsonFieldWriteable

A constraint on a BsonValue.

Dependencies

Properties that trigger additional constraints when present.

Enum

class Enum<T>(val values: List<Pair<T, KType>>, factory: BsonFactory) : Schema.SimpleConstraint<T> 

This property must have a value that is equal to one of the given values.

Maximum

class Maximum(
    val number: Number, 
    val numberType: KType, 
    val exclusive: Boolean, 
    factory: BsonFactory
) : Schema.ComplexConstraint<Number> 

This property must have a value less than or equal to number.

MaxItems

class MaxItems(val size: Int, factory: BsonFactory) : Schema.SimpleConstraint<Collection<*>> 

The maximum number of items in an array (inclusive).

MaxLength

class MaxLength(val length: Int, factory: BsonFactory) : Schema.SimpleConstraint<String> 

This property must have a value with a length less than or equal to length.

MaxProperties

class MaxProperties(val count: Int, factory: BsonFactory) : Schema.SimpleConstraint<Any> 

The maximum number of properties in an object (inclusive).

Minimum

class Minimum(
    val number: Number, 
    val numberType: KType, 
    val exclusive: Boolean, 
    factory: BsonFactory
) : Schema.ComplexConstraint<Number> 

This property must have a value greater than or equal to number.

MinItems

class MinItems(val size: Int, factory: BsonFactory) : Schema.SimpleConstraint<Collection<*>> 

The minimum number of items in an array (inclusive).

MinLength

class MinLength(val length: Int, factory: BsonFactory) : Schema.SimpleConstraint<String> 

This property must have a value with a length greater than or equal to length.

MinProperties

class MinProperties(val count: Int, factory: BsonFactory) : Schema.SimpleConstraint<Any> 

The minimum number of properties in an object (inclusive).

MultipleOf

class MultipleOf(
    val number: Number, 
    val numberType: KType, 
    factory: BsonFactory
) : Schema.SimpleConstraint<Number> 

This property must have a value that, when divided by number, is an integer.

Not

class Not<T>(val schema: Schema<T>, factory: BsonFactory) : Schema.SimpleConstraint<T> 

OneOf

class OneOf<T>(val schemas: List<Schema<T>>, factory: BsonFactory) : Schema.SimpleConstraint<T> 

The value must confirm to exactly one of the given schemas.

Pattern

class Pattern(@Language(value = "JSRegexp") val pattern: String, factory: BsonFactory) : Schema.SimpleConstraint<String> 

This property must have a value that matches the regular expression pattern, in the ECMA 262 dialect.

PatternProperties

class PatternProperties(val properties: Map<String, Schema<*>>, factory: BsonFactory) : Schema.SimpleConstraint<Any> 

The properties that must be present in an object, declared by pattern.

Properties

class Properties(val properties: Map<String, Schema<*>>, factory: BsonFactory) : Schema.SimpleConstraint<Any> 

The properties that must be present in an object.

Required

class Required(val properties: Set<String>, factory: BsonFactory) : Schema.SimpleConstraint<Any> 

The properties that must be present in an object.

SimpleConstraint

abstract class SimpleConstraint<in T>(val name: String, factory: BsonFactory) : Schema.ComplexConstraint<T> 

Helper to create instances of Constraint that handle a single constraint named name.

Type

class Type(val bsonType: BsonType, factory: BsonFactory) : Schema.SimpleConstraint<Any?> 

This property must have a value of the given bsonType.

TypeNumber

This property must have a numeric value (BsonType.Double, BsonType.Int32, BsonType.Int64 or BsonType.Decimal128).

UniqueItems

Whether the array must contain unique items.

Properties

constraints

The different properties this document should have.

factory

The factory used to create the BSON values that appear in this schema.

title

val title: String

The title of the schema, used to differentiate it from other schemas.

Functions

toString

open override fun toString(): String

JSON representation of this object as a BSON document.

writeTo

@LowLevelApi
open override fun writeTo(writer: BsonFieldWriter)

Writes this object to the provided writer.