func()

in internal/azure/types/object_type.go [50:100]


func (t *ObjectType) Validate(body interface{}, path string) []error {
	if t == nil || body == nil {
		return []error{}
	}
	errors := make([]error, 0)
	// check body type
	bodyMap, ok := body.(map[string]interface{})
	if !ok {
		errors = append(errors, utils.ErrorMismatch(path, "object", fmt.Sprintf("%T", body)))
		return errors
	}
	// check properties defined in body, but not in schema
	for key, value := range bodyMap {
		if def, ok := t.Properties[key]; ok {
			if def.IsReadOnly() {
				errors = append(errors, utils.ErrorShouldNotDefineReadOnly(path+"."+key))
				continue
			}
			var valueDefType *TypeBase
			if def.Type != nil && def.Type.Type != nil {
				valueDefType = def.Type.Type
				errors = append(errors, (*valueDefType).Validate(value, path+"."+key)...)
			}
			continue
		}
		if t.AdditionalProperties != nil && t.AdditionalProperties.Type != nil {
			errors = append(errors, (*t.AdditionalProperties.Type).Validate(value, path+"."+key)...)
		} else {
			options := make([]string, 0)
			for key := range t.Properties {
				options = append(options, path+"."+key)
			}
			errors = append(errors, utils.ErrorShouldNotDefine(path+"."+key, options))
		}
	}

	// check properties required in schema, but not in body
	for key, value := range t.Properties {
		if !value.IsRequired() {
			continue
		}
		if _, ok := bodyMap[key]; !ok {
			// skip name in body
			if path == "" && key == "name" {
				continue
			}
			errors = append(errors, utils.ErrorShouldDefine(path+"."+key))
		}
	}
	return errors
}