builders

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2025 License: Apache-2.0 Imports: 9 Imported by: 2

Documentation

Overview

datetime.go - Date/Time validation builder for Queryfy

dependent.go - Dependent fields validation for Queryfy

transform.go - Transformation support for Queryfy

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AndSchema

type AndSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

AndSchema validates that all schemas pass.

func And

func And(schemas ...queryfy.Schema) *AndSchema

And creates a new AND schema that requires all sub-schemas to pass.

func (*AndSchema) Nullable

func (s *AndSchema) Nullable() *AndSchema

Nullable allows the field to be null.

func (*AndSchema) Optional

func (s *AndSchema) Optional() *AndSchema

Optional marks the field as optional (default).

func (*AndSchema) Required

func (s *AndSchema) Required() *AndSchema

Required marks the field as required.

func (*AndSchema) Type

func (s *AndSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*AndSchema) Validate

func (s *AndSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type ArraySchema

type ArraySchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

ArraySchema validates array/slice values.

func Array

func Array() *ArraySchema

Array creates a new array schema builder.

func (*ArraySchema) Custom

Custom adds a custom validator function.

func (*ArraySchema) Length

func (s *ArraySchema) Length(length int) *ArraySchema

Length sets both minimum and maximum items to the same value.

func (*ArraySchema) MaxItems

func (s *ArraySchema) MaxItems(max int) *ArraySchema

MaxItems sets the maximum number of items.

func (*ArraySchema) MinItems

func (s *ArraySchema) MinItems(min int) *ArraySchema

MinItems sets the minimum number of items.

func (*ArraySchema) Nullable

func (s *ArraySchema) Nullable() *ArraySchema

Nullable allows the field to be null.

func (*ArraySchema) Of

func (s *ArraySchema) Of(schema queryfy.Schema) *ArraySchema

Of sets the schema for array elements.

func (*ArraySchema) Optional

func (s *ArraySchema) Optional() *ArraySchema

Optional marks the field as optional (default).

func (*ArraySchema) Required

func (s *ArraySchema) Required() *ArraySchema

Required marks the field as required.

func (*ArraySchema) Transform added in v0.2.0

func (s *ArraySchema) Transform(transformer Transformer) *TransformSchema

Transform adds transformation capability to ArraySchema.

func (*ArraySchema) Type

func (s *ArraySchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*ArraySchema) UniqueItems

func (s *ArraySchema) UniqueItems() *ArraySchema

UniqueItems requires all items to be unique.

func (*ArraySchema) Validate

func (s *ArraySchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type BoolSchema

type BoolSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

BoolSchema validates boolean values.

func Bool

func Bool() *BoolSchema

Bool creates a new boolean schema builder.

func (*BoolSchema) Custom

func (s *BoolSchema) Custom(fn queryfy.ValidatorFunc) *BoolSchema

Custom adds a custom validator function.

func (*BoolSchema) Nullable

func (s *BoolSchema) Nullable() *BoolSchema

Nullable allows the field to be null.

func (*BoolSchema) Optional

func (s *BoolSchema) Optional() *BoolSchema

Optional marks the field as optional (default).

func (*BoolSchema) Required

func (s *BoolSchema) Required() *BoolSchema

Required marks the field as required.

func (*BoolSchema) Transform added in v0.2.0

func (s *BoolSchema) Transform(transformer Transformer) *TransformSchema

Transform adds transformation capability to BoolSchema.

func (*BoolSchema) Type

func (s *BoolSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*BoolSchema) Validate

func (s *BoolSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type CustomSchema

type CustomSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

CustomSchema allows custom validation logic.

func Custom

func Custom(validator queryfy.ValidatorFunc) *CustomSchema

Custom creates a new custom schema with a validator function.

func (*CustomSchema) Nullable

func (s *CustomSchema) Nullable() *CustomSchema

Nullable allows the field to be null.

func (*CustomSchema) Optional

func (s *CustomSchema) Optional() *CustomSchema

Optional marks the field as optional (default).

func (*CustomSchema) Required

func (s *CustomSchema) Required() *CustomSchema

Required marks the field as required.

func (*CustomSchema) Type

func (s *CustomSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*CustomSchema) Validate

func (s *CustomSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type DateTimeSchema

type DateTimeSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

DateTimeSchema validates date/time values.

func DateTime

func DateTime() *DateTimeSchema

DateTime creates a new date/time schema builder.

func (*DateTimeSchema) Age

func (s *DateTimeSchema) Age(minAge, maxAge int) *DateTimeSchema

Age validates that the date represents an age within the specified range. Useful for birth dates.

func (*DateTimeSchema) Between

func (s *DateTimeSchema) Between(min, max time.Time) *DateTimeSchema

Between sets both minimum and maximum date/time.

func (*DateTimeSchema) BetweenStrings

func (s *DateTimeSchema) BetweenStrings(min, max string) *DateTimeSchema

BetweenStrings sets both minimum and maximum date/time from strings.

func (*DateTimeSchema) BusinessDay

func (s *DateTimeSchema) BusinessDay() *DateTimeSchema

BusinessDay validates that the date is a business day (Mon-Fri).

func (*DateTimeSchema) Custom

Custom adds a custom validator function.

func (*DateTimeSchema) DMY

func (s *DateTimeSchema) DMY() *DateTimeSchema

DMY sets the format to DD/MM/YYYY (used in UK, Europe, Latin America, and most of the world).

func (*DateTimeSchema) DateOnly

func (s *DateTimeSchema) DateOnly() *DateTimeSchema

DateOnly sets the format to date only (YYYY-MM-DD).

func (*DateTimeSchema) Format

func (s *DateTimeSchema) Format(format string) *DateTimeSchema

Format sets the expected date/time format. Common formats:

  • time.RFC3339: "2006-01-02T15:04:05Z07:00"
  • time.DateOnly: "2006-01-02"
  • "2006-01-02 15:04:05"
  • "01/02/2006"
  • "02-Jan-2006"

func (*DateTimeSchema) Future

func (s *DateTimeSchema) Future() *DateTimeSchema

Future requires the date/time to be in the future.

func (*DateTimeSchema) ISO8601

func (s *DateTimeSchema) ISO8601() *DateTimeSchema

ISO8601 sets the format to ISO8601 (RFC3339).

func (*DateTimeSchema) MDY

func (s *DateTimeSchema) MDY() *DateTimeSchema

MDY sets the format to MM/DD/YYYY (used primarily in the US).

func (*DateTimeSchema) Max

func (s *DateTimeSchema) Max(max time.Time) *DateTimeSchema

Max sets the maximum allowed date/time.

func (*DateTimeSchema) MaxString

func (s *DateTimeSchema) MaxString(max string) *DateTimeSchema

MaxString sets the maximum allowed date/time from a string.

func (*DateTimeSchema) Min

func (s *DateTimeSchema) Min(min time.Time) *DateTimeSchema

Min sets the minimum allowed date/time.

func (*DateTimeSchema) MinString

func (s *DateTimeSchema) MinString(min string) *DateTimeSchema

MinString sets the minimum allowed date/time from a string.

func (*DateTimeSchema) Nullable

func (s *DateTimeSchema) Nullable() *DateTimeSchema

Nullable allows the field to be null.

func (*DateTimeSchema) Optional

func (s *DateTimeSchema) Optional() *DateTimeSchema

Optional marks the field as optional (default).

func (*DateTimeSchema) Past

func (s *DateTimeSchema) Past() *DateTimeSchema

Past requires the date/time to be in the past.

func (*DateTimeSchema) Required

func (s *DateTimeSchema) Required() *DateTimeSchema

Required marks the field as required.

func (*DateTimeSchema) Transform added in v0.2.0

func (s *DateTimeSchema) Transform(transformer Transformer) *TransformSchema

Transform adds transformation capability to DateTimeSchema.

func (*DateTimeSchema) Type

func (s *DateTimeSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*DateTimeSchema) Validate

func (s *DateTimeSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

func (*DateTimeSchema) Weekday

func (s *DateTimeSchema) Weekday(days ...time.Weekday) *DateTimeSchema

Weekday validates that the date falls on specific weekdays.

func (*DateTimeSchema) YMD

func (s *DateTimeSchema) YMD() *DateTimeSchema

YMD sets the format to YYYY-MM-DD (ISO format).

type DependencyCondition added in v0.2.0

type DependencyCondition func(parentData map[string]interface{}) bool

DependencyCondition is a function that receives the parent object and returns whether the dependent validation should be applied.

func WhenAll added in v0.2.0

func WhenAll(conditions ...DependencyCondition) DependencyCondition

WhenAll creates a condition that requires all sub-conditions to be true.

func WhenAny added in v0.2.0

func WhenAny(conditions ...DependencyCondition) DependencyCondition

WhenAny creates a condition that requires at least one sub-condition to be true.

func WhenEquals added in v0.2.0

func WhenEquals(field string, value interface{}) DependencyCondition

WhenEquals creates a condition that checks if a field equals a specific value.

func WhenExists added in v0.2.0

func WhenExists(field string) DependencyCondition

WhenExists creates a condition that checks if a field exists and is not nil.

func WhenFalse added in v0.2.0

func WhenFalse(field string) DependencyCondition

WhenFalse creates a condition that checks if a boolean field is false.

func WhenGreaterThan added in v0.2.0

func WhenGreaterThan(field string, threshold float64) DependencyCondition

WhenGreaterThan creates a condition for numeric comparisons.

func WhenIn added in v0.2.0

func WhenIn(field string, values ...interface{}) DependencyCondition

WhenIn creates a condition that checks if a field's value is in a list.

func WhenLessThan added in v0.2.0

func WhenLessThan(field string, threshold float64) DependencyCondition

WhenLessThan creates a condition for numeric comparisons.

func WhenNotEquals added in v0.2.0

func WhenNotEquals(field string, value interface{}) DependencyCondition

WhenNotEquals creates a condition that checks if a field does not equal a specific value.

func WhenNotExists added in v0.2.0

func WhenNotExists(field string) DependencyCondition

WhenNotExists creates a condition that checks if a field does not exist or is nil.

func WhenTrue added in v0.2.0

func WhenTrue(field string) DependencyCondition

WhenTrue creates a condition that checks if a boolean field is true.

type DependentSchema added in v0.2.0

type DependentSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

DependentSchema validates fields based on conditions from other fields.

func Dependent added in v0.2.0

func Dependent(fieldName string) *DependentSchema

Dependent creates a new dependent field schema.

func RequiredUnless added in v0.2.0

func RequiredUnless(condition DependencyCondition, schema queryfy.Schema) *DependentSchema

RequiredUnless creates a schema that makes a field required unless a condition is met.

func RequiredWhen added in v0.2.0

func RequiredWhen(condition DependencyCondition, schema queryfy.Schema) *DependentSchema

RequiredWhen creates a schema that makes a field required when a condition is met.

func (*DependentSchema) Custom added in v0.2.0

Custom adds a custom validator function.

func (*DependentSchema) Else added in v0.2.0

func (s *DependentSchema) Else(elseSchema queryfy.Schema) *DependentSchema

Else sets the schema to apply when the condition is false.

func (*DependentSchema) On added in v0.2.0

func (s *DependentSchema) On(fields ...string) *DependentSchema

On specifies which fields this validation depends on.

func (*DependentSchema) Optional added in v0.2.0

func (s *DependentSchema) Optional() *DependentSchema

Optional marks the field as optional.

func (*DependentSchema) Required added in v0.2.0

func (s *DependentSchema) Required() *DependentSchema

Required marks the field as required (when the condition is met).

func (*DependentSchema) Then added in v0.2.0

func (s *DependentSchema) Then(schema queryfy.Schema) *DependentSchema

Then sets the schema to apply when the condition is true.

func (*DependentSchema) Type added in v0.2.0

Type implements the Schema interface.

func (*DependentSchema) Validate added in v0.2.0

func (s *DependentSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

func (*DependentSchema) ValidateWithParent added in v0.2.0

func (s *DependentSchema) ValidateWithParent(value interface{}, parentData map[string]interface{}, ctx *queryfy.ValidationContext) error

ValidateWithParent validates the field considering the parent object context.

func (*DependentSchema) When added in v0.2.0

When sets the condition function that determines when validation applies.

type NotSchema

type NotSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

NotSchema inverts the result of another schema.

func Not

func Not(schema queryfy.Schema) *NotSchema

Not creates a new NOT schema that inverts another schema's validation.

func (*NotSchema) Nullable

func (s *NotSchema) Nullable() *NotSchema

Nullable allows the field to be null.

func (*NotSchema) Optional

func (s *NotSchema) Optional() *NotSchema

Optional marks the field as optional (default).

func (*NotSchema) Required

func (s *NotSchema) Required() *NotSchema

Required marks the field as required.

func (*NotSchema) Type

func (s *NotSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*NotSchema) Validate

func (s *NotSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type NumberSchema

type NumberSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

NumberSchema validates numeric values.

func Number

func Number() *NumberSchema

Number creates a new number schema builder.

func (*NumberSchema) Custom

Custom adds a custom validator function.

func (*NumberSchema) Integer

func (s *NumberSchema) Integer() *NumberSchema

Integer validates that the number is an integer (no decimal part).

func (*NumberSchema) Max

func (s *NumberSchema) Max(max float64) *NumberSchema

Max sets the maximum value (inclusive).

func (*NumberSchema) Min

func (s *NumberSchema) Min(min float64) *NumberSchema

Min sets the minimum value (inclusive).

func (*NumberSchema) MultipleOf

func (s *NumberSchema) MultipleOf(value float64) *NumberSchema

MultipleOf validates that the number is a multiple of the given value.

func (*NumberSchema) Negative

func (s *NumberSchema) Negative() *NumberSchema

Negative validates that the number is negative (< 0).

func (*NumberSchema) Nullable

func (s *NumberSchema) Nullable() *NumberSchema

Nullable allows the field to be null.

func (*NumberSchema) Optional

func (s *NumberSchema) Optional() *NumberSchema

Optional marks the field as optional (default).

func (*NumberSchema) Positive

func (s *NumberSchema) Positive() *NumberSchema

Positive validates that the number is positive (> 0).

func (*NumberSchema) Range

func (s *NumberSchema) Range(min, max float64) *NumberSchema

Range sets both minimum and maximum values.

func (*NumberSchema) Required

func (s *NumberSchema) Required() *NumberSchema

Required marks the field as required.

func (*NumberSchema) Transform added in v0.2.0

func (s *NumberSchema) Transform(transformer Transformer) *TransformSchema

Transform adds transformation capability to NumberSchema.

func (*NumberSchema) Type

func (s *NumberSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*NumberSchema) Validate

func (s *NumberSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type ObjectSchema

type ObjectSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

ObjectSchema validates object/map values.

func Object

func Object() *ObjectSchema

Object creates a new object schema builder.

func (*ObjectSchema) Custom

Custom adds a custom validator function.

func (*ObjectSchema) Field

func (s *ObjectSchema) Field(name string, schema queryfy.Schema) *ObjectSchema

Field adds a field schema to the object.

func (*ObjectSchema) FieldNames

func (s *ObjectSchema) FieldNames() []string

FieldNames returns the names of all defined fields.

func (*ObjectSchema) Fields

func (s *ObjectSchema) Fields(fields map[string]queryfy.Schema) *ObjectSchema

Fields adds multiple field schemas at once.

func (*ObjectSchema) Nullable

func (s *ObjectSchema) Nullable() *ObjectSchema

Nullable allows the field to be null.

func (*ObjectSchema) Optional

func (s *ObjectSchema) Optional() *ObjectSchema

Optional marks the object as optional (default).

func (*ObjectSchema) Required

func (s *ObjectSchema) Required() *ObjectSchema

Required marks the object itself as required.

func (*ObjectSchema) RequiredFields

func (s *ObjectSchema) RequiredFields(names ...string) *ObjectSchema

RequiredFields marks specific fields as required. This overrides the required status set on individual field schemas.

func (*ObjectSchema) String

func (s *ObjectSchema) String() string

String returns a string representation of the object schema.

func (*ObjectSchema) Transform added in v0.2.0

func (s *ObjectSchema) Transform(transformer Transformer) *TransformSchema

Transform adds transformation capability to ObjectSchema.

func (*ObjectSchema) Type

func (s *ObjectSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*ObjectSchema) Validate

func (s *ObjectSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

func (*ObjectSchema) WithDependencies added in v0.2.0

func (s *ObjectSchema) WithDependencies() *ObjectSchemaWithDependencies

WithDependencies converts an ObjectSchema to support dependent field validation.

type ObjectSchemaWithDependencies added in v0.2.0

type ObjectSchemaWithDependencies struct {
	*ObjectSchema
	// contains filtered or unexported fields
}

ObjectSchemaWithDependencies extends the regular ObjectSchema to support dependent fields.

func (*ObjectSchemaWithDependencies) Custom added in v0.2.0

Custom adds a custom validator (override to return correct type).

func (*ObjectSchemaWithDependencies) DependentField added in v0.2.0

DependentField adds a dependent field to the schema.

func (*ObjectSchemaWithDependencies) Field added in v0.2.0

Field adds a regular field to the schema (override to return correct type).

func (*ObjectSchemaWithDependencies) Fields added in v0.2.0

Fields adds multiple fields at once (override to return correct type).

func (*ObjectSchemaWithDependencies) Nullable added in v0.2.0

Nullable allows the object to be null (override to return correct type).

func (*ObjectSchemaWithDependencies) Optional added in v0.2.0

Optional marks the object as optional (override to return correct type).

func (*ObjectSchemaWithDependencies) Required added in v0.2.0

Required marks the object as required (override to return correct type).

func (*ObjectSchemaWithDependencies) RequiredFields added in v0.2.0

RequiredFields marks specific fields as required (override to return correct type).

func (*ObjectSchemaWithDependencies) Validate added in v0.2.0

func (s *ObjectSchemaWithDependencies) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate overrides the base validate to handle dependent fields.

type OrSchema

type OrSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

OrSchema validates that at least one schema passes.

func Or

func Or(schemas ...queryfy.Schema) *OrSchema

Or creates a new OR schema that requires at least one sub-schema to pass.

func (*OrSchema) Nullable

func (s *OrSchema) Nullable() *OrSchema

Nullable allows the field to be null.

func (*OrSchema) Optional

func (s *OrSchema) Optional() *OrSchema

Optional marks the field as optional (default).

func (*OrSchema) Required

func (s *OrSchema) Required() *OrSchema

Required marks the field as required.

func (*OrSchema) Type

func (s *OrSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*OrSchema) Validate

func (s *OrSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type StringSchema

type StringSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

StringSchema validates string values.

func String

func String() *StringSchema

String creates a new string schema builder.

func (*StringSchema) Custom

Custom adds a custom validator function.

func (*StringSchema) Email

func (s *StringSchema) Email() *StringSchema

Email validates that the string is a valid email address.

func (*StringSchema) Enum

func (s *StringSchema) Enum(values ...string) *StringSchema

Enum restricts the string to one of the specified values.

func (*StringSchema) Length

func (s *StringSchema) Length(length int) *StringSchema

Length sets both minimum and maximum length to the same value.

func (*StringSchema) MaxLength

func (s *StringSchema) MaxLength(max int) *StringSchema

MaxLength sets the maximum string length.

func (*StringSchema) MinLength

func (s *StringSchema) MinLength(min int) *StringSchema

MinLength sets the minimum string length.

func (*StringSchema) Nullable

func (s *StringSchema) Nullable() *StringSchema

Nullable allows the field to be null.

func (*StringSchema) Optional

func (s *StringSchema) Optional() *StringSchema

Optional marks the field as optional (default).

func (*StringSchema) Pattern

func (s *StringSchema) Pattern(pattern string) *StringSchema

Pattern sets a regular expression pattern that the string must match.

func (*StringSchema) Required

func (s *StringSchema) Required() *StringSchema

Required marks the field as required.

func (*StringSchema) Transform added in v0.2.0

func (s *StringSchema) Transform(transformer Transformer) *TransformSchema

Transform adds transformation capability to StringSchema.

func (*StringSchema) Type

func (s *StringSchema) Type() queryfy.SchemaType

Type implements the Schema interface.

func (*StringSchema) URL

func (s *StringSchema) URL() *StringSchema

URL validates that the string is a valid URL.

func (*StringSchema) UUID

func (s *StringSchema) UUID() *StringSchema

UUID validates that the string is a valid UUID.

func (*StringSchema) Validate

func (s *StringSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

type TransformSchema added in v0.2.0

type TransformSchema struct {
	queryfy.BaseSchema
	// contains filtered or unexported fields
}

TransformSchema adds transformation capabilities to any schema.

func Transform added in v0.2.0

func Transform(schema queryfy.Schema) *TransformSchema

Transform creates a new transform schema wrapping an existing schema.

func (*TransformSchema) Add added in v0.2.0

func (s *TransformSchema) Add(transformer Transformer) *TransformSchema

Add adds a transformer to the pipeline.

func (*TransformSchema) Optional added in v0.2.0

func (s *TransformSchema) Optional() *TransformSchema

Optional marks the field as optional.

func (*TransformSchema) Required added in v0.2.0

func (s *TransformSchema) Required() *TransformSchema

Required marks the field as required.

func (*TransformSchema) Type added in v0.2.0

Type implements the Schema interface.

func (*TransformSchema) Validate added in v0.2.0

func (s *TransformSchema) Validate(value interface{}, ctx *queryfy.ValidationContext) error

Validate implements the Schema interface.

func (*TransformSchema) ValidateAndTransform added in v0.2.0

func (s *TransformSchema) ValidateAndTransform(value interface{}, ctx *queryfy.ValidationContext) (interface{}, error)

ValidateAndTransform validates and returns the transformed value.

type Transformer added in v0.2.0

type Transformer func(value interface{}) (interface{}, error)

Transformer is a function that transforms a value. It returns the transformed value and any error.

Directories

Path Synopsis
common.go - Common transformation functions
common.go - Common transformation functions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL