Documentation ¶
Index ¶
- Variables
- func NewValidationError(correlationId string, message string, results []*ValidationResult) *errors.ApplicationError
- func NewValidationErrorFromResults(correlationId string, results []*ValidationResult, strict bool) *errors.ApplicationError
- func ThrowValidationErrorIfNeeded(correlationId string, results []*ValidationResult, strict bool)
- type AndRule
- type ArraySchema
- type AtLeastOneExistsRule
- type ExcludedRule
- type ISchema
- type ISchemaBase
- type IValidationRule
- type IncludedRule
- type MapSchema
- type NotRule
- type ObjectSchema
- func (c *ObjectSchema) AllowUndefined(value bool) *ObjectSchema
- func (c *ObjectSchema) PerformValidation(path string, value any) []*ValidationResult
- func (c *ObjectSchema) Properties() []*PropertySchema
- func (c *ObjectSchema) SetProperties(value []*PropertySchema)
- func (c *ObjectSchema) SetUndefinedAllowed(value bool)
- func (c *ObjectSchema) UndefinedAllowed() bool
- func (c *ObjectSchema) WithOptionalProperty(name string, typ any, rules ...IValidationRule) *ObjectSchema
- func (c *ObjectSchema) WithProperty(schema *PropertySchema) *ObjectSchema
- func (c *ObjectSchema) WithRequiredProperty(name string, typ any, rules ...IValidationRule) *ObjectSchema
- type OnlyOneExistsRule
- type OrRule
- type PropertiesComparisonRule
- type PropertySchema
- type Schema
- func (c *Schema) MakeOptional() *Schema
- func (c *Schema) MakeRequired() *Schema
- func (c *Schema) PerformTypeValidation(path string, typ any, value any) []*ValidationResult
- func (c *Schema) PerformValidation(path string, value any) []*ValidationResult
- func (c *Schema) Required() bool
- func (c *Schema) Rules() []IValidationRule
- func (c *Schema) SetRequired(value bool)
- func (c *Schema) SetRules(value []IValidationRule)
- func (c *Schema) Validate(value any) []*ValidationResult
- func (c *Schema) ValidateAndReturnError(correlationId string, value any, strict bool) *errors.ApplicationError
- func (c *Schema) ValidateAndThrowError(correlationId string, value any, strict bool)
- func (c *Schema) WithRule(rule IValidationRule) *Schema
- type ValidationResult
- type ValidationResultType
- type ValueComparisonRule
Constants ¶
This section is empty.
Variables ¶
var ObjectComparator = &_TObjectComparator{}
ObjectComparator Helper class to perform comparison operations over arbitrary values.
Example: ObjectComparator.Compare(2, "GT", 1); // Result: true ObjectComparator.AreEqual("A", "B"); // Result: false
Functions ¶
func NewValidationError ¶
func NewValidationError(correlationId string, message string, results []*ValidationResult) *errors.ApplicationError
NewValidationError creates a new instance of validation exception and assigns its values.
see ValidationResult Parameters: - correlationId string - message string a human-readable description of the error. - results: []*ValidationResult a list of validation results Returns: *errors.ApplicationError
func NewValidationErrorFromResults ¶
func NewValidationErrorFromResults(correlationId string, results []*ValidationResult, strict bool) *errors.ApplicationError
NewValidationErrorFromResults creates a new ValidationError based on errors in validation results. If validation results have no errors, than null is returned.
see ValidationResult Parameters: - correlationId string transaction id to trace execution through call chain. - results []*ValidationResult list of validation results that may contain errors - strict boolean true to treat warnings as errors. Returns *errors.ApplicationError a newly created ValidationException or null if no errors in found.
func ThrowValidationErrorIfNeeded ¶
func ThrowValidationErrorIfNeeded(correlationId string, results []*ValidationResult, strict bool)
ThrowValidationErrorIfNeeded throws ValidationException based on errors in validation results. If validation results have no errors, than no exception is thrown.
see ValidationResult see ValidationException Parameters: - correlationId string transaction id to trace execution through call chain. - results []*ValidationResult list of validation results that may contain errors - strict bool true to treat warnings as errors.
Types ¶
type AndRule ¶
type AndRule struct {
// contains filtered or unexported fields
}
AndRule validation rule to combine rules with AND logical operation. When all rules returns no errors, than this rule also returns no errors. When one of the rules return errors, than the rules returns all errors.
Example: schema = NewSchema() .WithRule(NewAndRule( NewValueComparisonRule("GTE", 1), NewValueComparisonRule("LTE", 10), )); schema.Validate(0); // Result: 0 must be greater or equal to 1 schema.Validate(5); // Result: no error schema.Validate(20); // Result: 20 must be letter or equal 10
func NewAndRule ¶
func NewAndRule(rules ...IValidationRule) *AndRule
NewAndRule creates a new validation rule and sets its values.
Parameters: rules ...IValidationRule a list of rules to join with AND operator Returns: *AndRule
func (*AndRule) Validate ¶
func (c *AndRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates a given value against this rule.
Parameters: - path string a dot notation path to the value. - schema ISchema a schema this rule is called from - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
type ArraySchema ¶
type ArraySchema struct { *Schema // contains filtered or unexported fields }
ArraySchema to validate arrays.
Example: schema := NewArraySchema(TypeCode.String); schema.Validate(["A", "B", "C"]); // Result: no errors schema.Validate([1, 2, 3]); // Result: element type mismatch schema.Validate("A"); // Result: type mismatch
func NewArraySchema ¶
func NewArraySchema(valueType any) *ArraySchema
NewArraySchema creates a new instance of validation schema and sets its values.
see TypeCode Parameters: valueType any a type of array elements. Null means that elements may have any type. Returns: *ArraySchema
func NewProjectionParamsSchema ¶
func NewProjectionParamsSchema() *ArraySchema
NewProjectionParamsSchema creates a new instance of validation schema.
Returns: *ArraySchema
func (*ArraySchema) PerformValidation ¶
func (c *ArraySchema) PerformValidation(path string, value any) []*ValidationResult
PerformValidation validates a given value against the schema and configured validation rules.
Parameters: - path string a dot notation path to the value. - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
func (*ArraySchema) SetValueType ¶
func (c *ArraySchema) SetValueType(value any)
SetValueType sets the type of array elements. Null means that elements may have any type.
Parameters: value any a type of array elements.
func (*ArraySchema) ValueType ¶
func (c *ArraySchema) ValueType() any
ValueType gets the type of array elements. Null means that elements may have any type.
Returns: any the type of array elements.
type AtLeastOneExistsRule ¶
type AtLeastOneExistsRule struct {
// contains filtered or unexported fields
}
AtLeastOneExistsRule validation rule that check that at least one of the object properties is not null.
see IValidationRule Example: schema := NewSchema() .WithRule(NewAtLeastOneExistsRule("field1", "field2")) schema.Validate(struct { field1 int field2 string }{field1: 1, field2: "A"}) // Result: no errors schema.Validate(struct { field1 int field2 string }{field1: 1}) // Result: no errors schema.Validate(struct {}{}) // Result: at least one of properties field1, field2 must exist
func NewAtLeastOneExistsRule ¶
func NewAtLeastOneExistsRule(properties ...string) *AtLeastOneExistsRule
NewAtLeastOneExistsRule creates a new validation rule and sets its values
Parameters: properties ...string a list of property names where at least one property must exist Returns: *AtLeastOneExistsRule
func (*AtLeastOneExistsRule) Validate ¶
func (c *AtLeastOneExistsRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates a given value against this rule.
Parameters: - path string a dot notation path to the value. - schema ISchema a schema this rule is called from - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
type ExcludedRule ¶
type ExcludedRule struct {
// contains filtered or unexported fields
}
ExcludedRule validation rule to check that value is excluded from the list of constants.
see IValidationRule Example: schema := NewSchema() .WithRule(NewExcludedRule(1, 2, 3)); schema.Validate(2); // Result: 2 must not be one of 1, 2, 3 schema.Validate(10); // Result: no errors
func NewExcludedRule ¶
func NewExcludedRule(values ...any) *ExcludedRule
NewExcludedRule creates a new validation rule and sets its values.
Parameters: values ...any a list of constants that value must be excluded from Returns: *ExcludedRule
func (*ExcludedRule) Validate ¶
func (c *ExcludedRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates the given value. None of the values set in this ExcludedRule object must exist in the value that is given for validation to pass.
Parameters: - path string the dot notation path to the value that is to be validated. - schema ISchema (not used in this implementation). - value any the value that is to be validated. Returns: [*]ValidationResult the results of the validation.
type ISchema ¶
type ISchema interface { Validate(value any) []*ValidationResult ValidateAndReturnError(correlationId string, value any, strict bool) *errors.ApplicationError ValidateAndThrowError(correlationId string, value any, strict bool) }
ISchema validation schema interface
type ISchemaBase ¶
type ISchemaBase interface {
PerformValidation(path string, value any) []*ValidationResult
}
type IValidationRule ¶
type IValidationRule interface { // Validate a given value against this rule. // Parameters: // - path string a dot notation path to the value. // - schema ISchema a schema this rule is called from // - value any a value to be validated. // Returns: []*ValidationResult a list with validation results to add new results. Validate(path string, schema ISchema, value any) []*ValidationResult }
IValidationRule interface for validation rules. Validation rule can validate one or multiple values against complex rules like: value is in range, one property is less than another property, enforce enumerated values and more. This interface allows implementing custom rules.
type IncludedRule ¶
type IncludedRule struct {
// contains filtered or unexported fields
}
IncludedRule validation rule to check that value is included into the list of constants.
see IValidationRule Example: var schema = NewSchema() .WithRule(NewIncludedRule(1, 2, 3)); schema.Validate(2); // Result: no errors schema.Validate(10); // Result: 10 must be one of 1, 2, 3
func NewIncludedRule ¶
func NewIncludedRule(values ...any) *IncludedRule
NewIncludedRule creates a new validation rule and sets its values.
Parameters: values ...any a list of constants that value must be included to Returns: *IncludedRule
func (*IncludedRule) Validate ¶
func (c *IncludedRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates a given value against this rule.
Parameters: - path string a dot notation path to the value. - schema ISchema a schema this rule is called from - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
type MapSchema ¶
type MapSchema struct { *Schema // contains filtered or unexported fields }
MapSchema to validate maps.
Example var schema = NewMapSchema(TypeCode.String, TypeCode.Integer); schema.Validate({ "key1": "A", "key2": "B" }); // Result: no errors schema.Validate({ "key1": 1, "key2": 2 }); // Result: element type mismatch schema.Validate([ 1, 2, 3 ]); // Result: type mismatch
func NewFilterParamsSchema ¶
func NewFilterParamsSchema() *MapSchema
NewFilterParamsSchema creates a new instance of validation schema.
Returns: *MapSchema
func NewMapSchema ¶
NewMapSchema creates a new instance of validation schema and sets its values.
see IValidationRule see TypeCode Parameters: - keyType any a type of map keys. Null means that keys may have any type. - valueType any a type of map values. Null means that values may have any type. Returns: *MapSchema
func NewMapSchemaWithRules ¶
func NewMapSchemaWithRules(keyType any, valueType any, required bool, rules []IValidationRule) *MapSchema
NewMapSchemaWithRules creates a new instance of validation schema and sets its values.
see IValidationRule see TypeCode Parameters: - keyType any a type of map keys. Null means that keys may have any type. - valueType any a type of map values. Null means that values may have any type. - required: boolean true to always require non-null values. - rules: []IValidationRule a list with validation rules. Returns: *MapSchema
func (*MapSchema) KeyType ¶
KeyType gets the type of map keys. Null means that keys may have any type.
Returns: any the type of map keys.
func (*MapSchema) PerformValidation ¶
func (c *MapSchema) PerformValidation(path string, value any) []*ValidationResult
PerformValidation validates a given value against the schema and configured validation rules.
Parameters: - path string a dot notation path to the value. - value any a value to be validated. Returns: []*ValidationResult[] a list with validation results to add new results.
func (*MapSchema) SetKeyType ¶
SetKeyType sets the type of map keys. Null means that keys may have any type.
Parameters: value any a type of map keys.
func (*MapSchema) SetValueType ¶
SetValueType sets the type of map values. Null means that values may have any type.
Parameters: value any a type of map values.
type NotRule ¶
type NotRule struct {
// contains filtered or unexported fields
}
NotRule validation rule negate another rule. When embedded rule returns no errors, than this rule return an error. When embedded rule return errors, than the rule returns no errors.
see IValidationRule Example: var schema = NewSchema() .WithRule(NewNotRule( NewValueComparisonRule("EQ", 1), )); schema.Validate(1); // Result: error schema.Validate(5); // Result: no error
func NewNotRule ¶
func NewNotRule(rule IValidationRule) *NotRule
NewNotRule creates a new validation rule and sets its values
Parameters: rule IValidationRule a rule to be negated. Returns: *NotRule
func (*NotRule) Validate ¶
func (c *NotRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates a given value against this rule.
Parameters: - path string a dot notation path to the value. - schema ISchema a schema this rule is called from - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
type ObjectSchema ¶
type ObjectSchema struct { *Schema // contains filtered or unexported fields }
ObjectSchema to validate user defined objects.
Example: schema.Validate(struct { id string name string }{id: "1", name: "ABC"}) // Result: no errors schema.Validate(struct { id string name string }{name: "ABC"}) // Result: no errors schema.Validate(struct { id int name string }{id: 1, name: "ABC"}) // Result: id type mismatch schema.Validate(struct { id int _name string }{id: 1, _name: "ABC"}) // Result: name is missing, unexpected_name schema.Validate("ABC")
func NewObjectSchema ¶
func NewObjectSchema() *ObjectSchema
NewObjectSchema creates a new validation schema and sets its values.
Returns: *ObjectSchema
func NewObjectSchemaWithRules ¶
func NewObjectSchemaWithRules(allowUndefined bool, required bool, rules []IValidationRule) *ObjectSchema
NewObjectSchemaWithRules creates a new validation schema and sets its values.
see IValidationRule Parameters: - allowUndefined bool true to allow properties undefines in the schema - required bool true to always require non-null values. - rules []IValidationRule a list with validation rules. Returns: *ObjectSchema
func NewPagingParamsSchema ¶
func NewPagingParamsSchema() *ObjectSchema
NewPagingParamsSchema creates a new instance of validation schema.
Returns: *PagingParamsSchema
func NewTokenizedPagingParamsSchema ¶ added in v1.0.4
func NewTokenizedPagingParamsSchema() *ObjectSchema
NewTokenizedPagingParamsSchema creates a new instance of validation schema.
Returns: *TokenizedPagingParamsSchema
func (*ObjectSchema) AllowUndefined ¶
func (c *ObjectSchema) AllowUndefined(value bool) *ObjectSchema
AllowUndefined sets flag to allow undefined properties This method returns reference to this exception to implement Builder pattern to chain additional calls.
Parameters: value bool true to allow undefined properties and false to disallow. Returns: *ObjectSchema this validation schema.
func (*ObjectSchema) PerformValidation ¶
func (c *ObjectSchema) PerformValidation(path string, value any) []*ValidationResult
PerformValidation validates a given value against the schema and configured validation rules.
Parameters: - path string a dot notation path to the value. - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
func (*ObjectSchema) Properties ¶
func (c *ObjectSchema) Properties() []*PropertySchema
Properties gets validation schemas for object properties.
see PropertySchema Returns: []*PropertySchema the list of property validation schemas.
func (*ObjectSchema) SetProperties ¶
func (c *ObjectSchema) SetProperties(value []*PropertySchema)
SetProperties sets validation schemas for object properties.
see PropertySchema Parameters: value []*PropertySchema a list of property validation schemas.
func (*ObjectSchema) SetUndefinedAllowed ¶
func (c *ObjectSchema) SetUndefinedAllowed(value bool)
SetUndefinedAllowed gets flag to allow undefined properties
Parameters: value bool true to allow undefined properties and false to disallow.
func (*ObjectSchema) UndefinedAllowed ¶
func (c *ObjectSchema) UndefinedAllowed() bool
UndefinedAllowed gets flag to allow undefined properties
Returns: bool true to allow undefined properties and false to disallow.
func (*ObjectSchema) WithOptionalProperty ¶
func (c *ObjectSchema) WithOptionalProperty(name string, typ any, rules ...IValidationRule) *ObjectSchema
WithOptionalProperty adds a validation schema for an optional object property.
Parameters: - name string a property name. - type any a property schema or type. - rules ...IValidationRule a list of property validation rules. Returns: *ObjectSchema
func (*ObjectSchema) WithProperty ¶
func (c *ObjectSchema) WithProperty(schema *PropertySchema) *ObjectSchema
WithProperty adds a validation schema for an object property. This method returns reference to this exception to implement Builder pattern to chain additional calls.
see PropertySchema Parameters: schema *PropertySchema a property validation schema to be added. Returns: *ObjectSchema this validation schema.
func (*ObjectSchema) WithRequiredProperty ¶
func (c *ObjectSchema) WithRequiredProperty(name string, typ any, rules ...IValidationRule) *ObjectSchema
WithRequiredProperty adds a validation schema for a required object property.
Parameters: - name string a property name. - type any a property schema or type. - rules ...IValidationRule a list of property validation rules. Returns: *ObjectSchema
type OnlyOneExistsRule ¶
type OnlyOneExistsRule struct {
// contains filtered or unexported fields
}
OnlyOneExistsRule validation rule that check that at exactly one of the object properties is not null.
see IValidationRule Example var schema = NewSchema().WithRule(NewOnlyOneExistsRule("field1", "field2")) schema.Validate(struct { field1 int field2 string }{field1: 1, field2: "A"}) // Result: only one of properties field1, field2 must exist schema.Validate(struct{ field1 int }{field1: 1}) // Result: no errors schema.Validate(struct{}{})
func NewOnlyOneExistsRule ¶
func NewOnlyOneExistsRule(properties ...string) *OnlyOneExistsRule
NewOnlyOneExistsRule creates a new validation rule and sets its values
Parameters: properties ...string a list of property names where at only one property must exist Returns: *OnlyOneExistsRule
func (*OnlyOneExistsRule) Validate ¶
func (c *OnlyOneExistsRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates a given value against this rule.
Parameters: - path string a dot notation path to the value. - schema ISchema a schema this rule is called from - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
type OrRule ¶
type OrRule struct {
// contains filtered or unexported fields
}
OrRule validation rule to combine rules with OR logical operation. When one of rules returns no errors, then this rule also returns no errors. When all rules return errors, then the rule returns all errors.
see IValidationRule Example: var schema = NewObjectSchema().WithProperty(NewPropertySchema("id", TypeCode.String)) schema.Validate(struct { id string name string }{id: "1", name: "ABC"}) // Result: no errors schema.Validate(struct { id string name string }{name: "ABC"}) // Result: no errors schema.Validate(struct { id int name string }{id: 1, name: "ABC"}) // Result: id type mismatch
func NewOrRule ¶
func NewOrRule(rules ...IValidationRule) *OrRule
NewOrRule creates a new validation rule and ses its values
Parameters: rule IValidationRule a rule to be negated. Returns: *OrRule
func (*OrRule) Validate ¶
func (c *OrRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates a given value against this rule.
Parameters: - path string a dot notation path to th value. - schema ISchema a schema this rule is called from - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
type PropertiesComparisonRule ¶
type PropertiesComparisonRule struct {
// contains filtered or unexported fields
}
PropertiesComparisonRule validation rule that compares two object properties.
see IValidationRule Example: var schema = NewObjectSchema() .WithRule(NewPropertyComparisonRule("field1", "NE", "field2")); schema.Validate({ field1: 1, field2: 2 }); // Result: no errors schema.Validate({ field1: 1, field2: 1 }); // Result: field1 shall not be equal to field2 schema.Validate({}); // Result: no errors
func NewPropertiesComparisonRule ¶
func NewPropertiesComparisonRule(property1 string, operation string, property2 string) *PropertiesComparisonRule
NewPropertiesComparisonRule creates a new validation rule and sets its arguments.
see ObjectComparator.Compare Parameters: - property1 string a name of the first property to compare. - operation string a comparison operation: "==" ("=", "EQ"), "!= " ("<>", "NE"); "<"/">" ("LT"/"GT"), "<="/">=" ("LE"/"GE"); "LIKE". - property2 string a name of the second property to compare. Returns: *PropertiesComparisonRule
func (*PropertiesComparisonRule) Validate ¶
func (c *PropertiesComparisonRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Validate validates a given value against this rule.
Parameters: - path string a dot notation path to the value. - schema a schema this rule is called from - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
type PropertySchema ¶
type PropertySchema struct { *Schema // contains filtered or unexported fields }
PropertySchema to validate object properties
see ObjectSchema Example var schema = NewObjectSchema() .WithProperty(NewPropertySchema("id", TypeCode.String)); schema.Validate({ id: "1", name: "ABC" }); // Result: no errors schema.Validate({ name: "ABC" }); // Result: no errors schema.Validate({ id: 1, name: "ABC" }); // Result: id type mismatch
func NewPropertySchema ¶
func NewPropertySchema() *PropertySchema
Creates a new validation schema and sets its values. Returns *PropertySchema
func NewPropertySchemaWithRules ¶
func NewPropertySchemaWithRules(name string, typ any, required bool, rules []IValidationRule) *PropertySchema
NewPropertySchemaWithRules creates a new validation schema and sets its values.
see IValidationRule see TypeCode Parameters: - name string a property name - type any a property type - required bool true to always require non-null values. - rules []IValidationRule a list with validation rules. Returns: *PropertySchema
func (*PropertySchema) Name ¶
func (c *PropertySchema) Name() string
Name gets the property name.
Returns: string the property name.
func (*PropertySchema) PerformValidation ¶
func (c *PropertySchema) PerformValidation(path string, value any) []*ValidationResult
PerformValidation validates a given value against the schema and configured validation rules.
Parameters: - path string a dot notation path to the value. - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
func (*PropertySchema) SetName ¶
func (c *PropertySchema) SetName(value string)
SetName sets the property name.
Parameters: value string a new property name.
func (*PropertySchema) SetType ¶
func (c *PropertySchema) SetType(value any)
SetType sets a new property type. The type can be defined as type, type name or TypeCode
Parameters: value any a new property type.
func (*PropertySchema) Type ¶
func (c *PropertySchema) Type() any
Type gets the property type.
Returns: any the property type.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema basic schema that validates values against a set of validation rules. This schema is used as a basis for specific schemas to validate objects, project properties, arrays and maps.
func InheritSchema ¶
func InheritSchema(base ISchemaBase) *Schema
InheritSchema inherit schema
Parameters: base ISchemaBase base foe create new schema Returns: *Schema
func InheritSchemaWithRules ¶
func InheritSchemaWithRules(base ISchemaBase, required bool, rules []IValidationRule) *Schema
InheritSchemaWithRules inherit schema with rules
Parameters: - base ISchemaBase base foe create new schema - required bool true to always require non-null values. - rules []IValidationRule a list with validation rules. Returns: *Schema
func NewSchema ¶
func NewSchema() *Schema
NewSchema creates a new instance of validation schema and sets its values.
Returns: *Schema
func NewSchemaWithRules ¶
func NewSchemaWithRules(required bool, rules []IValidationRule) *Schema
NewSchemaWithRules creates a new instance of validation schema and sets its values.
see IValidationRule Parameters: - required bool true to always require non-null values. - rules []IValidationRule a list with validation rules. Returns: *Schema
func (*Schema) MakeOptional ¶
MakeOptional makes validated values optional. Validation for null values will be skipped. This method returns reference to this exception to implement Builder pattern to chain additional calls.
see MakeRequired Returns: *Schema this validation schema
func (*Schema) MakeRequired ¶
MakeRequired makes validated values always required (non-null). For null values the schema will raise errors. This method returns reference to this exception to implement Builder pattern to chain additional calls.
see MakeOptional Returns: *Schema this validation schema
func (*Schema) PerformTypeValidation ¶
func (c *Schema) PerformTypeValidation(path string, typ any, value any) []*ValidationResult
PerformTypeValidation validates a given value to match specified type. The type can be defined as a Schema, type, a type name or TypeCode When type is a Schema, it executes validation recursively against that Schema.
see PerformValidation Parameters: - path string a dot notation path to the value. - type any a type to match the value type - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
func (*Schema) PerformValidation ¶
func (c *Schema) PerformValidation(path string, value any) []*ValidationResult
PerformValidation validates a given value against the schema and configured validation rules.
Parameters: - path string a dot notation path to the value. - value any a value to be validated. Returns: []*ValidationResult a list with validation results to add new results.
func (*Schema) Required ¶
Required gets a flag that always requires non-null values. For null values it raises a validation error.
Returns: bool true to always require non-null values and false to allow null values.
func (*Schema) Rules ¶
func (c *Schema) Rules() []IValidationRule
Rules gets validation rules to check values against.
Returns: []IValidationRule a list with validation rules.
func (*Schema) SetRequired ¶
SetRequired sets a flag that always requires non-null values.
Parameters: value bool true to always require non-null values and false to allow null values.
func (*Schema) SetRules ¶
func (c *Schema) SetRules(value []IValidationRule)
SetRules sets validation rules to check values against.
Parameters: value []IValidationRule a list with validation rules.
func (*Schema) Validate ¶
func (c *Schema) Validate(value any) []*ValidationResult
Validate validates the given value and results validation results.
see ValidationResult Parameters: value any a value to be validated. Returns: []*ValidationResult a list with validation results.
func (*Schema) ValidateAndReturnError ¶
func (c *Schema) ValidateAndReturnError(correlationId string, value any, strict bool) *errors.ApplicationError
ValidateAndReturnError validates the given value and returns a *errors.ApplicationError if errors were found.
Parameters: - correlationId string transaction id to trace execution through call chain. - value any a value to be validated. - strict bool true to treat warnings as errors. Returns: *errors.ApplicationError
func (*Schema) ValidateAndThrowError ¶
ValidateAndThrowError validates the given value and throws a *errors.ApplicationError if errors were found.
see errors.ApplicationError.ThrowExceptionIfNeeded Parameters: - correlationId string transaction id to trace execution through call chain. - value any a value to be validated. - strict: bool true to treat warnings as errors.
func (*Schema) WithRule ¶
func (c *Schema) WithRule(rule IValidationRule) *Schema
WithRule adds validation rule to this schema. This method returns reference to this exception to implement Builder pattern to chain additional calls.
Parameters: rule IValidationRule a validation rule to be added. Returns: Schema this validation schema.
type ValidationResult ¶
type ValidationResult struct {
// contains filtered or unexported fields
}
ValidationResult result generated by schema validation
func NewValidationResult ¶
func NewValidationResult(path string, typ ValidationResultType, code string, message string, expected any, actual any) *ValidationResult
NewValidationResult creates a new instance of validation ressult and sets its values.
see ValidationResultType Parameters: - path string a dot notation path of the validated element. - type: ValidationResultType a type of the validation result: Information, Warning, or Error. - code string an error code. - message string a human-readable message. - expected any a value expected by schema validation. - actual any an actual value found by schema validation. Returns: *ValidationResult
func (*ValidationResult) Actual ¶
func (c *ValidationResult) Actual() any
Actual gets the actual value found by schema validation.
Returns: any the actual value.
func (*ValidationResult) Code ¶
func (c *ValidationResult) Code() string
Code gets the error code.
Returns: string the error code
func (*ValidationResult) Expected ¶
func (c *ValidationResult) Expected() any
Expected gets the value expected by schema validation.
Returns: any the expected value.
func (*ValidationResult) Message ¶
func (c *ValidationResult) Message() string
Message gets the human readable message.
Returns: string the result message.
func (*ValidationResult) Path ¶
func (c *ValidationResult) Path() string
Path gets dot notation path of the validated element.
Returns: string the path of the validated element.
func (*ValidationResult) Type ¶
func (c *ValidationResult) Type() ValidationResultType
Type gets the type of the validation result: Information, Warning, or Error.
see ValidationResultType Returns: ValidationResultType the type of the validation result.
type ValidationResultType ¶
type ValidationResultType int
ValidationResultType types of validation results generated by validation schemas.
Information = 0 General information (not an error). Warning = 1 Warning about something suspecious. In strict mode is treated as error Error = 2 - Validation error.
const ( Information ValidationResultType = iota Warning Error )
type ValueComparisonRule ¶
type ValueComparisonRule struct {
// contains filtered or unexported fields
}
ValueComparisonRule validation rule that compares value to a constant.
see IValidationRule Example: var schema = NewSchema() .WithRule(NewValueComparisonRule("EQ", 1)); schema.Validate(1); // Result: no errors schema.Validate(2); // Result: 2 is not equal to 1
func NewValueComparisonRule ¶
func NewValueComparisonRule(operation string, value any) *ValueComparisonRule
NewValueComparisonRule creates a new validation rule and sets its values.
Parameters: - operation string a comparison operation: "==" ("=", "EQ"), "!= " ("<>", "NE"); "<"/">" ("LT"/"GT"), "<="/">=" ("LE"/"GE"); "LIKE". - value any a constant value to compare to Returns: *ValueComparisonRule
func (*ValueComparisonRule) Validate ¶
func (c *ValueComparisonRule) Validate(path string, schema ISchema, value any) []*ValidationResult
Source Files ¶
- AndRule.go
- ArraySchema.go
- AtLeastOneExistsRule.go
- ExcludedRule.go
- FilterParamsSchema.go
- ISchema.go
- ISchemaBase.go
- IValidationRule.go
- IncludedRule.go
- MapSchema.go
- NotRule.go
- ObjectComparator.go
- ObjectSchema.go
- OnlyOneExistsRule.go
- OrRule.go
- PagingParamsSchema.go
- ProjectionParamsSchema.go
- PropertiesComparisonRule.go
- PropertySchema.go
- Schema.go
- TokenizedPagingParamsSchema.go
- ValidationException.go
- ValidationResult.go
- ValidationResultType.go
- ValueComparisonRule.go
- doc.go