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

package models

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

import (
	"context"
	stderrors "errors"
	"strconv"

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

// V1alpha1Rule Identity Service Policy Rule
//
// swagger:model v1alpha1Rule
type V1alpha1Rule struct {

	// The action applied for the rule when calling the specified tasks
	// Required: true
	Action *V1alpha1RuleAction `json:"action"`

	// CreatedAt records the timestamp of when the Rule was initially created
	// Read Only: true
	// Format: date-time
	CreatedAt strfmt.DateTime `json:"createdAt,omitempty"`

	// A human-readable description for the Rule.
	Description string `json:"description,omitempty"`

	// A unique identifier for the Rule.
	// Read Only: true
	ID string `json:"id,omitempty"`

	// A human-readable name for the Rule.
	// Required: true
	Name *string `json:"name"`

	// Need User Approval for this Rule.
	// Required: true
	NeedsApproval *bool `json:"needsApproval"`

	// policy Id
	// Read Only: true
	PolicyID string `json:"policyId,omitempty"`

	// The tasks that this Rule applies to.
	// Required: true
	Tasks []*V1alpha1Task `json:"tasks"`
}

// Validate validates this v1alpha1 rule
func (m *V1alpha1Rule) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

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

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

func (m *V1alpha1Rule) validateAction(formats strfmt.Registry) error {

	if err := validate.Required("action", "body", m.Action); err != nil {
		return err
	}

	if err := validate.Required("action", "body", m.Action); err != nil {
		return err
	}

	if m.Action != nil {
		if err := m.Action.Validate(formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("action")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("action")
			}

			return err
		}
	}

	return nil
}

func (m *V1alpha1Rule) validateCreatedAt(formats strfmt.Registry) error {
	if swag.IsZero(m.CreatedAt) { // not required
		return nil
	}

	if err := validate.FormatOf("createdAt", "body", "date-time", m.CreatedAt.String(), formats); err != nil {
		return err
	}

	return nil
}

func (m *V1alpha1Rule) validateName(formats strfmt.Registry) error {

	if err := validate.Required("name", "body", m.Name); err != nil {
		return err
	}

	return nil
}

func (m *V1alpha1Rule) validateNeedsApproval(formats strfmt.Registry) error {

	if err := validate.Required("needsApproval", "body", m.NeedsApproval); err != nil {
		return err
	}

	return nil
}

func (m *V1alpha1Rule) validateTasks(formats strfmt.Registry) error {

	if err := validate.Required("tasks", "body", m.Tasks); err != nil {
		return err
	}

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

		if m.Tasks[i] != nil {
			if err := m.Tasks[i].Validate(formats); err != nil {
				ve := new(errors.Validation)
				if stderrors.As(err, &ve) {
					return ve.ValidateName("tasks" + "." + strconv.Itoa(i))
				}
				ce := new(errors.CompositeError)
				if stderrors.As(err, &ce) {
					return ce.ValidateName("tasks" + "." + strconv.Itoa(i))
				}

				return err
			}
		}

	}

	return nil
}

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

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

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

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

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

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

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

func (m *V1alpha1Rule) contextValidateAction(ctx context.Context, formats strfmt.Registry) error {

	if m.Action != nil {

		if err := m.Action.ContextValidate(ctx, formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("action")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("action")
			}

			return err
		}
	}

	return nil
}

func (m *V1alpha1Rule) contextValidateCreatedAt(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "createdAt", "body", m.CreatedAt); err != nil {
		return err
	}

	return nil
}

func (m *V1alpha1Rule) contextValidateID(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "id", "body", m.ID); err != nil {
		return err
	}

	return nil
}

func (m *V1alpha1Rule) contextValidatePolicyID(ctx context.Context, formats strfmt.Registry) error {

	if err := validate.ReadOnly(ctx, "policyId", "body", m.PolicyID); err != nil {
		return err
	}

	return nil
}

func (m *V1alpha1Rule) contextValidateTasks(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Tasks[i] != nil {

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

			if err := m.Tasks[i].ContextValidate(ctx, formats); err != nil {
				ve := new(errors.Validation)
				if stderrors.As(err, &ve) {
					return ve.ValidateName("tasks" + "." + strconv.Itoa(i))
				}
				ce := new(errors.CompositeError)
				if stderrors.As(err, &ce) {
					return ce.ValidateName("tasks" + "." + strconv.Itoa(i))
				}

				return err
			}
		}

	}

	return nil
}

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

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