// Code generated by go-swagger; DO NOT EDIT.

package ops

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"context"
	"strconv"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag"
	"github.com/go-openapi/validate"
)

// OperationOperation operation operation
//
// swagger:model operation.Operation
type OperationOperation struct {

	// Actions is actions be executed in an operation.
	Actions []*OperationOperationAction `json:"actions"`

	// Alias is the shown name of an operation when creating.
	Alias string `json:"alias,omitempty"`

	// approval cc
	ApprovalCc *OperationOperationApprovalCc `json:"approval_cc,omitempty"`

	// ApprovalTips is the text shown on approval page.
	ApprovalTips string `json:"approval_tips,omitempty"`

	// Approver defines who can approve this operation.
	// The index of array is the level of approver, max is 3.
	Approvers []*OperationOperationApprover `json:"approvers"`

	// Categories is the category of an operation.
	Categories []string `json:"categories"`

	// Description is the description of an operation.
	Description string `json:"description,omitempty"`

	// Internal means this operation is internal and only created by system.
	// It will not be shown on UI when internal is true.
	Internal bool `json:"internal,omitempty"`

	// Name is the unique identifier of an operation.
	Name string `json:"name,omitempty"`

	// Params is params of an operation, it will be used in actions.
	Params map[string]OperationParamDefine `json:"params,omitempty"`

	// target
	Target *OperationOperationTarget `json:"target,omitempty"`
}

// Validate validates this operation operation
func (m *OperationOperation) Validate(formats strfmt.Registry) error {
	var res []error

	if err := m.validateActions(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateApprovalCc(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateApprovers(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateParams(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateTarget(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *OperationOperation) validateActions(formats strfmt.Registry) error {
	if swag.IsZero(m.Actions) { // not required
		return nil
	}

	for i := 0; i < len(m.Actions); i++ {
		if swag.IsZero(m.Actions[i]) { // not required
			continue
		}

		if m.Actions[i] != nil {
			if err := m.Actions[i].Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("actions" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("actions" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

func (m *OperationOperation) validateApprovalCc(formats strfmt.Registry) error {
	if swag.IsZero(m.ApprovalCc) { // not required
		return nil
	}

	if m.ApprovalCc != nil {
		if err := m.ApprovalCc.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("approval_cc")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("approval_cc")
			}
			return err
		}
	}

	return nil
}

func (m *OperationOperation) validateApprovers(formats strfmt.Registry) error {
	if swag.IsZero(m.Approvers) { // not required
		return nil
	}

	for i := 0; i < len(m.Approvers); i++ {
		if swag.IsZero(m.Approvers[i]) { // not required
			continue
		}

		if m.Approvers[i] != nil {
			if err := m.Approvers[i].Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("approvers" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("approvers" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

func (m *OperationOperation) validateParams(formats strfmt.Registry) error {
	if swag.IsZero(m.Params) { // not required
		return nil
	}

	for k := range m.Params {

		if err := validate.Required("params"+"."+k, "body", m.Params[k]); err != nil {
			return err
		}
		if val, ok := m.Params[k]; ok {
			if err := val.Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("params" + "." + k)
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("params" + "." + k)
				}
				return err
			}
		}

	}

	return nil
}

func (m *OperationOperation) validateTarget(formats strfmt.Registry) error {
	if swag.IsZero(m.Target) { // not required
		return nil
	}

	if m.Target != nil {
		if err := m.Target.Validate(formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("target")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("target")
			}
			return err
		}
	}

	return nil
}

// ContextValidate validate this operation operation based on the context it is used
func (m *OperationOperation) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	if err := m.contextValidateActions(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateApprovalCc(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateApprovers(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateParams(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateTarget(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *OperationOperation) contextValidateActions(ctx context.Context, formats strfmt.Registry) error {

	for i := 0; i < len(m.Actions); i++ {

		if m.Actions[i] != nil {

			if swag.IsZero(m.Actions[i]) { // not required
				return nil
			}

			if err := m.Actions[i].ContextValidate(ctx, formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("actions" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("actions" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

func (m *OperationOperation) contextValidateApprovalCc(ctx context.Context, formats strfmt.Registry) error {

	if m.ApprovalCc != nil {

		if swag.IsZero(m.ApprovalCc) { // not required
			return nil
		}

		if err := m.ApprovalCc.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("approval_cc")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("approval_cc")
			}
			return err
		}
	}

	return nil
}

func (m *OperationOperation) contextValidateApprovers(ctx context.Context, formats strfmt.Registry) error {

	for i := 0; i < len(m.Approvers); i++ {

		if m.Approvers[i] != nil {

			if swag.IsZero(m.Approvers[i]) { // not required
				return nil
			}

			if err := m.Approvers[i].ContextValidate(ctx, formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("approvers" + "." + strconv.Itoa(i))
				} else if ce, ok := err.(*errors.CompositeError); ok {
					return ce.ValidateName("approvers" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

func (m *OperationOperation) contextValidateParams(ctx context.Context, formats strfmt.Registry) error {

	for k := range m.Params {

		if val, ok := m.Params[k]; ok {
			if err := val.ContextValidate(ctx, formats); err != nil {
				return err
			}
		}

	}

	return nil
}

func (m *OperationOperation) contextValidateTarget(ctx context.Context, formats strfmt.Registry) error {

	if m.Target != nil {

		if swag.IsZero(m.Target) { // not required
			return nil
		}

		if err := m.Target.ContextValidate(ctx, formats); err != nil {
			if ve, ok := err.(*errors.Validation); ok {
				return ve.ValidateName("target")
			} else if ce, ok := err.(*errors.CompositeError); ok {
				return ce.ValidateName("target")
			}
			return err
		}
	}

	return nil
}

// MarshalBinary interface implementation
func (m *OperationOperation) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return swag.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *OperationOperation) UnmarshalBinary(b []byte) error {
	var res OperationOperation
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
