mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-03 06:44:50 -04:00
validation: add generic validation framework (#2480)
* [wip] validation framework Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * [wip] wip Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * working for shallow structs!!! Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix needle pointer deref Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add comment Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix nested structs Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix nested struct pointers Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add tests Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix slices / arrays Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix struct parsing Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * extend tests Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * expose API Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * extend in-package documentation Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * linter fixes Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * fix naming Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * add missing license headers Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Daniel Weiße <66256922+daniel-weisse@users.noreply.github.com> * align with review Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> --------- Signed-off-by: Moritz Sanft <58110325+msanft@users.noreply.github.com> Co-authored-by: Daniel Weiße <66256922+daniel-weisse@users.noreply.github.com>
This commit is contained in:
parent
2f745a2edb
commit
a104936bc6
6 changed files with 1184 additions and 0 deletions
48
internal/validation/validation.go
Normal file
48
internal/validation/validation.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
/*
|
||||
Package validation provides a unified document validation interface for use within the Constellation CLI.
|
||||
|
||||
It validates documents that specify a set of constraints on their content.
|
||||
*/
|
||||
package validation
|
||||
|
||||
import "errors"
|
||||
|
||||
// NewValidator creates a new Validator.
|
||||
func NewValidator() *Validator {
|
||||
return &Validator{}
|
||||
}
|
||||
|
||||
// Validator validates documents.
|
||||
type Validator struct{}
|
||||
|
||||
// Validatable is implemented by documents that can be validated.
|
||||
// It returns a list of constraints that must be satisfied for the document to be valid.
|
||||
type Validatable interface {
|
||||
Constraints() []Constraint
|
||||
}
|
||||
|
||||
// ValidateOptions are the options to use when validating a document.
|
||||
type ValidateOptions struct {
|
||||
// FailFast stops validation on the first error.
|
||||
FailFast bool
|
||||
}
|
||||
|
||||
// Validate validates a document using the given options.
|
||||
func (v *Validator) Validate(doc Validatable, opts ValidateOptions) error {
|
||||
var retErr error
|
||||
for _, c := range doc.Constraints() {
|
||||
if err := c.Satisfied(); err != nil {
|
||||
if opts.FailFast {
|
||||
return err
|
||||
}
|
||||
retErr = errors.Join(retErr, err)
|
||||
}
|
||||
}
|
||||
return retErr
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue