validate

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 8 Imported by: 31

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ObjectComparator = &TObjectComparator{}

Functions

func NewValidationError

func NewValidationError(correlationId string, message string, results []*ValidationResult) *errors.ApplicationError

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

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)

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
}

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

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 interface{}) []*ValidationResult

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 interface{} a value to be validated.

Return []*ValidationResult a list with validation results to add new results.

type ArraySchema

type ArraySchema struct {
	Schema
	// contains filtered or unexported fields
}

Schema 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 interface{}) *ArraySchema

Returns *ArraySchema

func NewProjectionParamsSchema

func NewProjectionParamsSchema() *ArraySchema

Creates a new instance of validation schema. Returns *ArraySchema

func (*ArraySchema) PerformValidation

func (c *ArraySchema) PerformValidation(path string, value interface{}) []*ValidationResult

Validates a given value against the schema and configured validation rules. Parameters:

  • path string a dot notation path to the value.
  • value interface{} a value to be validated.

Return []*ValidationResult a list with validation results to add new results.

func (*ArraySchema) SetValueType

func (c *ArraySchema) SetValueType(value interface{})

Sets the type of array elements. Null means that elements may have any type. Parameters value interface{} a type of array elements.

func (*ArraySchema) ValueType

func (c *ArraySchema) ValueType() interface{}

Gets the type of array elements. Null means that elements may have any type. Returns interface{} the type of array elements.

type AtLeastOneExistsRule

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

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({ field1: 1, field2: "A" });     // Result: no errors
schema.Validate({ field1: 1 });                  // Result: no errors
schema.Validate({ });                            // Result: at least one of properties field1, field2 must exist

func NewAtLeastOneExistsRule

func NewAtLeastOneExistsRule(properties ...string) *AtLeastOneExistsRule

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 interface{}) []*ValidationResult

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 interface{} a value to be validated.

Return []*ValidationResult a list with validation results to add new results.

type ExcludedRule

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

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 ...interface{}) *ExcludedRule

Creates a new validation rule and sets its values. Parameters:

values ...interface{}
a list of constants that value must be excluded from

Returns *ExcludedRule

func (*ExcludedRule) Validate

func (c *ExcludedRule) Validate(path string, schema ISchema, value interface{}) []*ValidationResult

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 interface{}
the value that is to be validated.

Return [*]ValidationResult the results of the validation.

type ISchema

type ISchema interface {
	Validate(value interface{}) []*ValidationResult
	ValidateAndReturnError(correlationId string, value interface{}, strict bool) *errors.ApplicationError
	ValidateAndThrowError(correlationId string, value interface{}, strict bool)
}

Validation schema interface

type ISchemaBase

type ISchemaBase interface {
	PerformValidation(path string, value interface{}) []*ValidationResult
}

type IValidationRule

type IValidationRule interface {
	// 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 interface{}y
	//  a value to be validated.
	// 	Returns []*ValidationResult
	// a list with validation results to add new results.
	Validate(path string, schema ISchema, value interface{}) []*ValidationResult
}

Interface for validation rules.

Vlidation 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 to implement custom rules.

type IncludedRule

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

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 ...interface{}) *IncludedRule

Creates a new validation rule and sets its values. Parameters:

  • values ...interface{} a list of constants that value must be included to

Returns *IncludedRule

func (*IncludedRule) Validate

func (c *IncludedRule) Validate(path string, schema ISchema, value interface{}) []*ValidationResult

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 interface{}y 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
}

Schema 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

Creates a new instance of validation schema. Returns *MapSchema

func NewMapSchema

func NewMapSchema(keyType interface{}, valueType interface{}) *MapSchema

Creates a new instance of validation schema and sets its values. see IValidationRule see TypeCode Parameters:

  • keyType interface{} a type of map keys. Null means that keys may have any type.
  • valueType interface{} a type of map values. Null means that values may have any type.

Returns *MapSchema

func NewMapSchemaWithRules

func NewMapSchemaWithRules(keyType interface{}, valueType interface{}, required bool, rules []IValidationRule) *MapSchema

Creates a new instance of validation schema and sets its values. see IValidationRule see TypeCode Parameters:

  • keyType interface{} a type of map keys. Null means that keys may have any type.
  • valueType interface{} 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

func (c *MapSchema) KeyType() interface{}

Gets the type of map keys. Null means that keys may have any type. Returns interface{} the type of map keys.

func (*MapSchema) PerformValidation

func (c *MapSchema) PerformValidation(path string, value interface{}) []*ValidationResult

Validates a given value against the schema and configured validation rules. Parameters:

  • path string a dot notation path to the value.
  • value interface{} a value to be validated.

REturns []*ValidationResult[] a list with validation results to add new results.

func (*MapSchema) SetKeyType

func (c *MapSchema) SetKeyType(value interface{})

Sets the type of map keys. Null means that keys may have any type. Parameters:

  • value interface{} a type of map keys.

func (*MapSchema) SetValueType

func (c *MapSchema) SetValueType(value interface{})

Sets the type of map values. Null means that values may have any type. Parameters:

  • value interface{} a type of map values.

func (*MapSchema) ValueType

func (c *MapSchema) ValueType() interface{}

Gets the type of map values. Null means that values may have any type. Returns interface{} the type of map values.

type NotRule

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

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

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 interface{}) []*ValidationResult

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 interface{} 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
}

// Schema to validate user defined objects.

// Example // var schema = NewObjectSchema(false) // .WithOptionalProperty("id", TypeCode.String) // .WithRequiredProperty("name", 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 // schema.validate({ id: 1, _name: "ABC" }); // Result: name is missing, unexpected _name // schema.validate("ABC"); // Result: type mismatch

func NewObjectSchema

func NewObjectSchema() *ObjectSchema

Creates a new validation schema and sets its values. Returns *ObjectSchema

func NewObjectSchemaWithRules

func NewObjectSchemaWithRules(allowUndefined bool, required bool, rules []IValidationRule) *ObjectSchema

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

Creates a new instance of validation schema. Returns *PagingParamsSchema

func NewTokenizedPagingParamsSchema added in v1.1.0

func NewTokenizedPagingParamsSchema() *ObjectSchema

Creates a new instance of validation schema. Returns *TokenizedPagingParamsSchema

func (*ObjectSchema) AllowUndefined

func (c *ObjectSchema) AllowUndefined(value bool) *ObjectSchema

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 interface{}) []*ValidationResult

Validates a given value against the schema and configured validation rules. Parameters:

  • path string a dot notation path to the value.
  • value interface{} a value to be validated.

Return []*ValidationResult a list with validation results to add new results.

func (*ObjectSchema) Properties

func (c *ObjectSchema) Properties() []*PropertySchema

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)

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)

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

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 interface{}, rules ...IValidationRule) *ObjectSchema

Adds a validation schema for an optional object property. Parameters:

  • name string a property name.
  • type interface{} 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

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 interface{}, rules ...IValidationRule) *ObjectSchema

Adds a validation schema for a required object property. Parameters:

  • name string a property name.
  • type interface{} 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
}

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({ field1: 1, field2: "A" });     // Result: only one of properties field1, field2 must exist
schema.Validate({ field1: 1 });                  // Result: no errors
schema.Validate({ });                            // Result: only one of properties field1, field2 must exist

func NewOnlyOneExistsRule

func NewOnlyOneExistsRule(properties ...string) *OnlyOneExistsRule

Creates a new validation rule and sets its values Parameters:

  • properties ...string

a list of property names where at only one property must exist Return *OnlyOneExistsRule

func (*OnlyOneExistsRule) Validate

func (c *OnlyOneExistsRule) Validate(path string, schema ISchema, value interface{}) []*ValidationResult

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 interface{} a value to be validated.

Retruns []*ValidationResult a list with validation results to add new results.

type OrRule

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

alidation rule to combine rules with OR logical operation. When one of rules returns no errors, than this rule also returns no errors. When all rules return errors, than the rule returns all errors.

see IValidationRule

Example

var schema = NewSchema()
    .WithRule(NewOrRule(
        NewValueComparisonRle("LT", 1),
        NewValueComparisonule("GT", 10)
    ));

schema.Validate();          // Result: no error
schema.Validate5);          // Result: 5 must be less than 1 or 5 must be more than 10
schema.Validate(20);        // Result: no error

func NewOrRule

func NewOrRule(rules ...IValidationRule) *OrRule

Creates a new validation rule and ses its values Parameters:

  • rule IValidationRule a rule to be negaed.

Returns *OrRule

func (*OrRule) Validate

func (c *OrRule) Validate(path string, schema ISchema, value interface{}) []*ValidationResult

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 interface{} a valueto be validated.

Retruns []*ValidationResult a list with validation results to add new results.

type PropertiesComparisonRule

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

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

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 interface{}) []*ValidationResult

Validates a given value against this rule. Parameters:

  • path string a dot notation path to the value.
  • schema Schema a schema this rule is called from
  • value interface{} a value to be validated.

Return []*ValidationResult a list with validation results to add new results.

type PropertySchema

type PropertySchema struct {
	Schema
	// contains filtered or unexported fields
}

Schema 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 interface{}, required bool, rules []IValidationRule) *PropertySchema

Creates a new validation schema and sets its values. see IValidationRule see TypeCode Parameters:

  • name string a property name
  • type interface{} 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

Gets the property name. Returns string the property name.

func (*PropertySchema) PerformValidation

func (c *PropertySchema) PerformValidation(path string, value interface{}) []*ValidationResult

Validates a given value against the schema and configured validation rules. Parameters:

  • path string a dot notation path to the value.
  • value interface{} a value to be validated.

Return []*ValidationResult a list with validation results to add new results.

func (*PropertySchema) SetName

func (c *PropertySchema) SetName(value string)

Sets the property name. Parameters:

  • value string a new property name.

func (*PropertySchema) SetType

func (c *PropertySchema) SetType(value interface{})

Sets a new property type. The type can be defined as type, type name or TypeCode Parameters:

  • value interface{} a new property type.

func (*PropertySchema) Type

func (c *PropertySchema) Type() interface{}

Gets the property type. Returns any the property type.

type Schema

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

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

Inherit schema Parameters:

  • base ISchemaBase base foe create new schema

Returns *Schema

func InheritSchemaWithRules

func InheritSchemaWithRules(base ISchemaBase, required bool, rules []IValidationRule) *Schema

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

Creates a new instance of validation schema and sets its values. Returns *Schema

func NewSchemaWithRules

func NewSchemaWithRules(required bool, rules []IValidationRule) *Schema

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

func (c *Schema) MakeOptional() *Schema

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

func (c *Schema) MakeRequired() *Schema

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 interface{}, value interface{}) []*ValidationResult

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 interface{} a type to match the value type value interface{} 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 interface{}) []*ValidationResult

Validates a given value against the schema and configured validation rules. Parameters:

  • path string a dot notation path to the value.
  • value interface{} a value to be validated.

Return []*ValidationResult a list with validation results to add new results.

func (*Schema) Required

func (c *Schema) Required() bool

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

Gets validation rules to check values against. Returns []IValidationRule a list with validation rules.

func (*Schema) SetRequired

func (c *Schema) SetRequired(value bool)

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)

Sets validation rules to check values against. Parameters:

  • value []IValidationRule a list with validation rules.

func (*Schema) Validate

func (c *Schema) Validate(value interface{}) []*ValidationResult

Validates the given value and results validation results. see ValidationResult Parameters:

  • value interface{} a value to be validated.

Returns []*ValidationResult a list with validation results.

func (*Schema) ValidateAndReturnError

func (c *Schema) ValidateAndReturnError(correlationId string, value interface{}, strict bool) *errors.ApplicationError

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 interface{} a value to be validated.
  • strict bool true to treat warnings as errors.

Returns *errors.ApplicationError

func (*Schema) ValidateAndThrowError

func (c *Schema) ValidateAndThrowError(correlationId string, value interface{}, strict bool)

Validates the given value and throws a *errors.ApplicationError if errors were found. see ApplicationError.throwExceptionIfNeeded Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • value interface{} a value to be validated.
  • strict: bool true to treat warnings as errors.

func (*Schema) WithRule

func (c *Schema) WithRule(rule IValidationRule) *Schema

Returns Schema this validation schema.

type TObjectComparator

type TObjectComparator struct{}

Helper class to perform comparison operations over arbitrary values.

Example

ObjectComparator.Compare(2, "GT", 1);        // Result: true
ObjectComparator.AreEqual("A", "B");         // Result: false

func (*TObjectComparator) AreEqual

func (c *TObjectComparator) AreEqual(value1 interface{}, value2 interface{}) bool

Checks if two values are equal. The operation can be performed over values of any type. Parameters:

  • value1 interface the first value to compare value2 interface{} the second value to compare

Returns bool true if values are equal and false otherwise

func (*TObjectComparator) AreNotEqual

func (c *TObjectComparator) AreNotEqual(value1 interface{}, value2 interface{}) bool

Checks if two values are NOT equal The operation can be performed over values of any type. Parameters:

  • value1 interface{} the first value to compare
  • value2 interface{} the second value to compare

Returns bool true if values are NOT equal and false otherwise

func (*TObjectComparator) Compare

func (c *TObjectComparator) Compare(value1 interface{}, operation string, value2 interface{}) bool

Perform comparison operation over two arguments. The operation can be performed over values of any type. Parameters:

  • value1 interface{} the first argument to compare operation string the comparison operation: "==" ("=", "EQ"), "!= " ("<>", "NE"); "<"/">" ("LT"/"GT"), "<="/">=" ("LE"/"GE"); "LIKE".
  • value2 interface{} the second argument to compare

Returns bool result of the comparison operation

func (*TObjectComparator) IsGreater

func (c *TObjectComparator) IsGreater(value1 interface{}, value2 interface{}) bool

Checks if first value is greater than the second one. The operation can be performed over numbers or strings. Parameters:

  • value1 interface{} the first value to compare
  • value2 interface{} the second value to compare

Returns bool true if the first value is greater than second and false otherwise.

func (*TObjectComparator) IsLess

func (c *TObjectComparator) IsLess(value1 interface{}, value2 interface{}) bool

Checks if first value is less than the second one. The operation can be performed over numbers or strings. Parameters:

  • value1 interface{} the first value to compare
  • value2 interface{} the second value to compare

Returns bool true if the first value is less than second and false otherwise.

func (*TObjectComparator) Match

func (c *TObjectComparator) Match(value1 interface{}, value2 interface{}) bool

Checks if string views are matches Parameters:

  • value1 interface{} a string value to match
  • value1 interface{} a string value to match

Returns bool true if the value matches regular expression and false otherwise.

type ValidationResult

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

Result generated by schema validation

func NewValidationResult

func NewValidationResult(path string, typ ValidationResultType, code string, message string,
	expected interface{}, actual interface{}) *ValidationResult

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 interface{} an value expected by schema validation.
  • actual interface{} an actual value found by schema validation.

Returns *ValidationResult

func (*ValidationResult) Actual

func (c *ValidationResult) Actual() interface{}

Gets the actual value found by schema validation. Returns any the actual value.

func (*ValidationResult) Code

func (c *ValidationResult) Code() string

Gets the error code. Returns string the error code

func (*ValidationResult) Expected

func (c *ValidationResult) Expected() interface{}

Returns any the expected value.

func (*ValidationResult) Message

func (c *ValidationResult) Message() string

Gets the human readable message. Returns string the result message.

func (*ValidationResult) Path

func (c *ValidationResult) Path() string

Gets dot notation path of the validated element. Returns string the path of the validated element.

func (*ValidationResult) 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

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
}

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 interface{}) *ValueComparisonRule

Creates a new validation rule and sets its values. Parameters:

  • operation string a comparison operation: "==" ("=", "EQ"), "!= " ("<>", "NE"); "<"/">" ("LT"/"GT"), "<="/">=" ("LE"/"GE"); "LIKE".
  • value interface{} a constant value to compare to

Returns *ValueComparisonRule

func (*ValueComparisonRule) Validate

func (c *ValueComparisonRule) Validate(path string, schema ISchema, value interface{}) []*ValidationResult

Jump to

Keyboard shortcuts

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