Implements: Schema
Description
The PropertySchema class allows you to create schemas to validate object properties.
Constructors
NewPropertySchemaWithRules
Creates a new validation rule and sets its arguments. See IValidationRule, TypeCode
NewPropertySchemaWithRules(name string, typ interface{}, required bool, rules []IValidationRule) *PropertySchema
- name: string - (optional) property name
- typ: interface{} - (optional) property type
- required: bool - (optional) true to always require non-nil values.
- rules: []IValidationRule - (optional) list with validation rules.
Creates a new validation schema and sets its values.
NewPropertySchema() *PropertySchema
Methods
Name
Gets the property name.
(c *PropertySchema) Name() string
- returns: string - the property name.
Type
Gets the property type.
(c *PropertySchema) Type() interface{}
- returns: interface{} - property type.
PerformValidation
Validates a given value against the schema and configured validation rules.
(c *PropertySchema) PerformValidation(path string, value interface{}) []*ValidationResult
- path: string - dot notation path to the value.
- value: interface{} - value to be validated.
- results: []*ValidationResult - list with validation results to add new results.
SetName
Sets the property name.
(c *PropertySchema) SetName(value string)
- value: string - new property name.
SetType
Sets a new property type. The type can be defined as type, type name or TypeCode
(c *PropertySchema) SetType(value interface{})
- value: interface{} - new property type.
Examples
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