// 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"
	"strconv"

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

// PolicyRules Rules for one interaction type.
//
// swagger:model PolicyRules
type PolicyRules struct {

	// Policy entries for accounts that can always do this type of interaction.
	Always []PolicyValue `json:"always"`

	// Policy entries for accounts that require approval to do this type of interaction.
	WithApproval []PolicyValue `json:"with_approval"`
}

// Validate validates this policy rules
func (m *PolicyRules) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

func (m *PolicyRules) validateAlways(formats strfmt.Registry) error {
	if swag.IsZero(m.Always) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *PolicyRules) validateWithApproval(formats strfmt.Registry) error {
	if swag.IsZero(m.WithApproval) { // not required
		return nil
	}

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

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

	}

	return nil
}

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

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

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

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

func (m *PolicyRules) contextValidateAlways(ctx context.Context, formats strfmt.Registry) error {

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

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

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

	}

	return nil
}

func (m *PolicyRules) contextValidateWithApproval(ctx context.Context, formats strfmt.Registry) error {

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

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

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

	}

	return nil
}

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

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