use

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package use provides common utilities and validations for document, along with middleware for relations, to be used in the `Init` function of the schema. For example:

func (p *PersonSchema) Init() {
	p.Use(use.BelongsTo[*PersonSchema](Place))
	p.Use(use.ValidatePresenceOf[string]("first_name"))
}

Relations:

  • BelongsTo
  • HasMany
  • HasOne

Utilities:

  • Validate Presence
  • Validate Uniqueness
  • Validate Format
  • Custom Validation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BelongsTo added in v0.7.0

func BelongsTo[Doc document[Doc], Rel document[Rel]](model *weasel.Model[Rel]) weasel.Middleware

use.BelongsTo populates the field that returns the belongs to relationship, specified in the schema. It takes a document and the model that the document belongs to.

func HasMany added in v0.7.0

func HasMany[Doc document[Doc], Rel document[Rel]](model *weasel.Model[Rel]) weasel.Middleware

use.HasMany populates the field that returns the has many relationship, specified in the schema. It takes a document and the model that the document has many of.

func HasOne added in v0.7.0

func HasOne[Doc document[Doc], Rel document[Rel]](model *weasel.Model[Rel]) weasel.Middleware

use.HasOne populates the field that returns the has one relationship, specified in the schema. it takes a document and the model that the document has one of.

func Validate

func Validate[T any](field string, validator func(val T) bool) weasel.Middleware

Validate takes a field and a function with a parameter as the value of that field returning a bool. It makes sure that the value satisfies the function, or else it adds an error to the model.

doc.Use(use.Validate("email", func (email string) bool {
	return email != "some@thing.com"
}))

func ValidateFormatOf

func ValidateFormatOf(field string, format *regexp.Regexp) weasel.Middleware

ValidateFormatOf takes a regular expression and checks that the field matches the pattern.

doc.Use(use.ValidateFormatOf("email", regexp.MustCompile(`[^@ \t\r\n]+@[^@ \t\r\n]+\.[^@ \t\r\n]+`)))

func ValidatePresenceOf

func ValidatePresenceOf[T any](field string) weasel.Middleware

ValidatePresenceOf takes a type parameter of the data type of the field and the field itself, and checks that the field exists in the record.

doc.Use(use.ValidatePresenceOf[string]("email"))

func ValidateUniqueCombination added in v0.7.1

func ValidateUniqueCombination(field1, field2 string, fields ...string) weasel.Middleware

ValidateUniqueCombination checks that the combination of fields is unique in the DB.

doc.Use(use.ValidateUniqueCombination("friend_id", "friender_id"))

func ValidateUniquenessOf

func ValidateUniquenessOf(field string) weasel.Middleware

ValidateUniquenessOf checks that the value for the field specified is unique in the DB. Do not use ValidateUniquenessOf on the Primary Key, as it will always pass. For example, the query would look like: SELECT COUNT(*) FROM table WHERE id = id AND id != id;

Use autoincrement or validate their uniqueness internally.

doc.Use(use.ValidateUniquenessOf("email"))

Types

This section is empty.

Jump to

Keyboard shortcuts

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