validators

package
v0.3.11-8 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2024 License: Apache-2.0 Imports: 29 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ValidateModel = func(ctx context.Context, userCred mcclient.TokenCredential, manager db.IStandaloneModelManager, id *string) (db.IModel, error) {
	if len(*id) == 0 {
		return nil, httperrors.NewMissingParameterError(manager.Keyword() + "_id")
	}

	model, err := manager.FetchByIdOrName(ctx, userCred, *id)
	if err != nil {
		if errors.Cause(err) == sql.ErrNoRows {
			return nil, httperrors.NewResourceNotFoundError2(manager.Keyword(), *id)
		}
		if errors.Cause(err) == sqlchemy.ErrDuplicateEntry {
			return nil, httperrors.NewDuplicateResourceError(manager.Keyword(), *id)
		}
		return nil, httperrors.NewGeneralError(err)
	}
	*id = model.GetId()
	return model, nil
}

Functions

func ApplyModelFilters

func ApplyModelFilters(ctx context.Context, q *sqlchemy.SQuery, data *jsonutils.JSONDict, opts []*ModelFilterOptions) (*sqlchemy.SQuery, error)

func IsModelNotFoundError

func IsModelNotFoundError(err error) bool

Types

type ActorIPv4Prefix

type ActorIPv4Prefix struct {
	// contains filtered or unexported fields
}

func NewActorIPv4Prefix

func NewActorIPv4Prefix() *ActorIPv4Prefix

func (*ActorIPv4Prefix) Set

func (actor *ActorIPv4Prefix) Set(data *jsonutils.JSONDict, key string, val interface{})

func (*ActorIPv4Prefix) Validate

func (actor *ActorIPv4Prefix) Validate(val interface{}) (bool, error)

type ActorJoinedBy

type ActorJoinedBy struct {
	// contains filtered or unexported fields
}

func NewActorJoinedBy

func NewActorJoinedBy(sep string, subActor IValidateActor) *ActorJoinedBy

func (*ActorJoinedBy) IgnoreEmpty

func (actor *ActorJoinedBy) IgnoreEmpty(b bool) *ActorJoinedBy

func (*ActorJoinedBy) Set

func (actor *ActorJoinedBy) Set(data *jsonutils.JSONDict, key string, val interface{})

func (*ActorJoinedBy) TrimSpace

func (actor *ActorJoinedBy) TrimSpace(b bool) *ActorJoinedBy

func (*ActorJoinedBy) Validate

func (actor *ActorJoinedBy) Validate(val interface{}) (bool, error)

type ErrType

type ErrType uintptr
const (
	ERR_SUCCESS ErrType = iota
	ERR_GENERAL         // uncategorized error
	ERR_MISSING_KEY
	ERR_INVALID_TYPE
	ERR_INVALID_CHOICE
	ERR_INVALID_LENGTH
	ERR_NOT_IN_RANGE
	ERR_INVALID_VALUE
	ERR_MODEL_MANAGER
	ERR_MODEL_NOT_FOUND
)

func (ErrType) String

func (errType ErrType) String() string

type IValidateActor

type IValidateActor interface {
	Validate(val interface{}) (bool, error)
	Set(data *jsonutils.JSONDict, key string, val interface{})
	// contains filtered or unexported methods
}

type IValidator

type IValidator interface {
	IValidatorBase
	Optional(bool) IValidator
	// contains filtered or unexported methods
}

type IValidatorBase

type IValidatorBase interface {
	Validate(ctx context.Context, data *jsonutils.JSONDict) error
}

type ModelFilterOptions

type ModelFilterOptions struct {
	Key          string
	ModelKeyword string
	OwnerId      mcclient.IIdentityProvider
}

type ValidateError

type ValidateError struct {
	ErrType ErrType
	Msg     string
}

func (*ValidateError) Cause

func (ve *ValidateError) Cause() error

func (*ValidateError) Error

func (ve *ValidateError) Error() string

type Validator

type Validator struct {
	Key string
	// contains filtered or unexported fields
}

func (*Validator) Default

func (v *Validator) Default(defaultVal interface{}) IValidator

func (*Validator) Optional

func (v *Validator) Optional(optional bool) IValidator

func (*Validator) SetParent

func (v *Validator) SetParent(parent IValidator) IValidator

func (*Validator) Validate

func (v *Validator) Validate(data *jsonutils.JSONDict) error

type ValidatorBool

type ValidatorBool struct {
	Validator
	Value bool
}

func NewBoolValidator

func NewBoolValidator(key string) *ValidatorBool

func (*ValidatorBool) Default

func (v *ValidatorBool) Default(i bool) IValidator

func (*ValidatorBool) Validate

func (v *ValidatorBool) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorByActor

type ValidatorByActor struct {
	Validator
	Value interface{}
	// contains filtered or unexported fields
}

func NewValidatorByActor

func NewValidatorByActor(key string, actor IValidateActor) *ValidatorByActor

func (*ValidatorByActor) Default

func (v *ValidatorByActor) Default(val interface{}) IValidator

func (*ValidatorByActor) Validate

func (v *ValidatorByActor) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorCertKey

type ValidatorCertKey struct {
	*ValidatorCertificate
	*ValidatorPrivateKey
	// contains filtered or unexported fields
}

func NewCertKeyValidator

func NewCertKeyValidator(cert, key string) *ValidatorCertKey

func (*ValidatorCertKey) UpdateCertKeyInfo

func (v *ValidatorCertKey) UpdateCertKeyInfo(ctx context.Context, data *jsonutils.JSONDict) *jsonutils.JSONDict

func (*ValidatorCertKey) Validate

func (v *ValidatorCertKey) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorCertificate

type ValidatorCertificate struct {
	ValidatorPEM
	Certificates []*x509.Certificate
}

func NewCertificateValidator

func NewCertificateValidator(key string) *ValidatorCertificate

func (*ValidatorCertificate) FingerprintSha256

func (v *ValidatorCertificate) FingerprintSha256() []byte

func (*ValidatorCertificate) FingerprintSha256String

func (v *ValidatorCertificate) FingerprintSha256String() string

func (*ValidatorCertificate) PublicKeyBitLen

func (v *ValidatorCertificate) PublicKeyBitLen() int

func (*ValidatorCertificate) Validate

func (v *ValidatorCertificate) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorDomainName

type ValidatorDomainName struct {
	ValidatorRegexp
}

func NewDomainNameValidator

func NewDomainNameValidator(key string) *ValidatorDomainName

type ValidatorFunc

type ValidatorFunc func(context.Context, *jsonutils.JSONDict) error

type ValidatorHostPort

type ValidatorHostPort struct {
	ValidatorRegexp

	Domain string
	Port   int
	Value  string
	// contains filtered or unexported fields
}

func NewHostPortValidator

func NewHostPortValidator(key string) *ValidatorHostPort

func (*ValidatorHostPort) OptionalPort

func (v *ValidatorHostPort) OptionalPort(optionalPort bool) *ValidatorHostPort

func (*ValidatorHostPort) Validate

func (v *ValidatorHostPort) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorIPv4Addr

type ValidatorIPv4Addr struct {
	Validator
	IP net.IP
}

func NewIPv4AddrValidator

func NewIPv4AddrValidator(key string) *ValidatorIPv4Addr

func (*ValidatorIPv4Addr) Validate

func (v *ValidatorIPv4Addr) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorIPv4Prefix

type ValidatorIPv4Prefix struct {
	Validator
	Value netutils.IPV4Prefix
}

func NewIPv4PrefixValidator

func NewIPv4PrefixValidator(key string) *ValidatorIPv4Prefix

func (*ValidatorIPv4Prefix) Validate

func (v *ValidatorIPv4Prefix) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorIntChoices

type ValidatorIntChoices struct {
	Validator

	Value int64
	// contains filtered or unexported fields
}

func NewIntChoicesValidator

func NewIntChoicesValidator(key string, choices []int64) *ValidatorIntChoices

func (*ValidatorIntChoices) Default

func (v *ValidatorIntChoices) Default(i int64) IValidator

func (*ValidatorIntChoices) Validate

func (v *ValidatorIntChoices) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorModelIdOrName

type ValidatorModelIdOrName struct {
	Validator
	ModelKeyword string
	OwnerId      mcclient.IIdentityProvider
	ModelManager db.IModelManager
	Model        db.IModel
	// contains filtered or unexported fields
}

func NewModelIdOrNameValidator

func NewModelIdOrNameValidator(key string, modelKeyword string, ownerId mcclient.IIdentityProvider) *ValidatorModelIdOrName

func (*ValidatorModelIdOrName) AllowEmpty

func (*ValidatorModelIdOrName) AllowPendingDeleted

func (v *ValidatorModelIdOrName) AllowPendingDeleted(b bool) *ValidatorModelIdOrName

AllowPendingDeleted allows the to-be-validated id or name to be of a pending deleted model

func (*ValidatorModelIdOrName) GetDomainId

func (v *ValidatorModelIdOrName) GetDomainId() string

func (*ValidatorModelIdOrName) GetDomainName

func (v *ValidatorModelIdOrName) GetDomainName() string

func (*ValidatorModelIdOrName) GetProjectDomain

func (v *ValidatorModelIdOrName) GetProjectDomain() string

func (*ValidatorModelIdOrName) GetProjectDomainId

func (v *ValidatorModelIdOrName) GetProjectDomainId() string

func (*ValidatorModelIdOrName) GetProjectId

func (v *ValidatorModelIdOrName) GetProjectId() string

func (*ValidatorModelIdOrName) GetProjectName

func (v *ValidatorModelIdOrName) GetProjectName() string

func (*ValidatorModelIdOrName) GetTenantId

func (v *ValidatorModelIdOrName) GetTenantId() string

func (*ValidatorModelIdOrName) GetTenantName

func (v *ValidatorModelIdOrName) GetTenantName() string

func (*ValidatorModelIdOrName) GetUserId

func (v *ValidatorModelIdOrName) GetUserId() string

func (*ValidatorModelIdOrName) GetUserName

func (v *ValidatorModelIdOrName) GetUserName() string

func (*ValidatorModelIdOrName) ModelIdKey

func (v *ValidatorModelIdOrName) ModelIdKey(modelIdKey string) *ValidatorModelIdOrName

func (*ValidatorModelIdOrName) QueryFilter

func (*ValidatorModelIdOrName) Validate

type ValidatorPEM

type ValidatorPEM struct {
	Validator
	Blocks []*pem.Block
}

func NewPEMValidator

func NewPEMValidator(key string) *ValidatorPEM

func (*ValidatorPEM) Validate

func (v *ValidatorPEM) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorPrivateKey

type ValidatorPrivateKey struct {
	ValidatorPEM
	PrivateKey crypto.PrivateKey
}

func NewPrivateKeyValidator

func NewPrivateKeyValidator(key string) *ValidatorPrivateKey

func (*ValidatorPrivateKey) MatchCertificate

func (v *ValidatorPrivateKey) MatchCertificate(cert *x509.Certificate) error

func (*ValidatorPrivateKey) Validate

func (v *ValidatorPrivateKey) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorRange

type ValidatorRange struct {
	Validator
	Lower int64
	Upper int64
	Value int64
}

func NewNonNegativeValidator

func NewNonNegativeValidator(key string) *ValidatorRange

func NewPortValidator

func NewPortValidator(key string) *ValidatorRange

func NewRangeValidator

func NewRangeValidator(key string, lower int64, upper int64) *ValidatorRange

func NewVlanIdValidator

func NewVlanIdValidator(key string) *ValidatorRange

func (*ValidatorRange) Default

func (v *ValidatorRange) Default(i int64) IValidator

func (*ValidatorRange) Validate

func (v *ValidatorRange) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorRegexp

type ValidatorRegexp struct {
	Validator
	Regexp *regexp.Regexp
	Value  string
	// contains filtered or unexported fields
}

func NewRegexpValidator

func NewRegexpValidator(key string, regexp *regexp.Regexp) *ValidatorRegexp

func (*ValidatorRegexp) AllowEmpty

func (v *ValidatorRegexp) AllowEmpty(allowEmpty bool) *ValidatorRegexp

func (*ValidatorRegexp) Validate

func (v *ValidatorRegexp) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorSSHKey

type ValidatorSSHKey struct {
	Validator
	Value  string
	Signer ssh.Signer
}

func NewSSHKeyValidator

func NewSSHKeyValidator(key string) *ValidatorSSHKey

func (*ValidatorSSHKey) Default

func (v *ValidatorSSHKey) Default(s string) IValidator

func (*ValidatorSSHKey) Validate

func (v *ValidatorSSHKey) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorStringChoices

type ValidatorStringChoices struct {
	Validator
	Choices choices.Choices
	Value   string
}

func NewStringChoicesValidator

func NewStringChoicesValidator(key string, choices choices.Choices) *ValidatorStringChoices

func (*ValidatorStringChoices) Default

func (*ValidatorStringChoices) Validate

type ValidatorStringLen

type ValidatorStringLen struct {
	Validator
	Value string
	// contains filtered or unexported fields
}

func NewMaxStringLenValidator

func NewMaxStringLenValidator(key string, maxLen int) *ValidatorStringLen

func NewMinStringLenValidator

func NewMinStringLenValidator(key string, minLen int) *ValidatorStringLen

func NewStringLenRangeValidator

func NewStringLenRangeValidator(key string, minLen, maxLen int) *ValidatorStringLen

func NewStringNonEmptyValidator

func NewStringNonEmptyValidator(key string) *ValidatorStringLen

func (*ValidatorStringLen) Default

func (v *ValidatorStringLen) Default(s string) IValidator

func (*ValidatorStringLen) Validate

func (v *ValidatorStringLen) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorStringMultiChoices

type ValidatorStringMultiChoices struct {
	Validator
	Choices choices.Choices
	Value   string
	// contains filtered or unexported fields
}

func NewStringMultiChoicesValidator

func NewStringMultiChoicesValidator(key string, choices choices.Choices) *ValidatorStringMultiChoices

func (*ValidatorStringMultiChoices) Default

func (*ValidatorStringMultiChoices) KeepDup

func (*ValidatorStringMultiChoices) Sep

func (*ValidatorStringMultiChoices) Validate

type ValidatorStruct

type ValidatorStruct struct {
	Validator
	Value interface{}
}

func NewStructValidator

func NewStructValidator(key string, value interface{}) *ValidatorStruct

func (*ValidatorStruct) Validate

func (v *ValidatorStruct) Validate(ctx context.Context, data *jsonutils.JSONDict) error

type ValidatorURLPath

type ValidatorURLPath struct {
	ValidatorRegexp
}

func NewURLPathValidator

func NewURLPathValidator(key string) *ValidatorURLPath

Jump to

Keyboard shortcuts

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