// 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"
	"github.com/go-openapi/validate"
)

// FilterV2 FilterV2 represents a user-defined filter for determining which statuses should not be shown to the user.
//
// v2 filters have names and can include multiple phrases and status IDs to filter.
//
// swagger:model FilterV2
type FilterV2 struct {

	// The contexts in which the filter should be applied.
	// Example: ["home","public"]
	// Min Items: 1
	// Unique: true
	Context []FilterContext `json:"context"`

	// When the filter should no longer be applied. Null if the filter does not expire.
	// Example: 2024-02-01T02:57:49Z
	ExpiresAt string `json:"expires_at,omitempty"`

	// The ID of the filter in the database.
	ID string `json:"id,omitempty"`

	// The keywords grouped under this filter.
	Keywords []*FilterKeyword `json:"keywords"`

	// The statuses grouped under this filter.
	Statuses []*FilterStatus `json:"statuses"`

	// The name of the filter.
	// Example: Linux Words
	Title string `json:"title,omitempty"`

	// filter action
	FilterAction FilterAction `json:"filter_action,omitempty"`
}

// Validate validates this filter v2
func (m *FilterV2) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

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

func (m *FilterV2) validateContext(formats strfmt.Registry) error {
	if swag.IsZero(m.Context) { // not required
		return nil
	}

	iContextSize := int64(len(m.Context))

	if err := validate.MinItems("context", "body", iContextSize, 1); err != nil {
		return err
	}

	if err := validate.UniqueItems("context", "body", m.Context); err != nil {
		return err
	}

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

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

	}

	return nil
}

func (m *FilterV2) validateKeywords(formats strfmt.Registry) error {
	if swag.IsZero(m.Keywords) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *FilterV2) validateStatuses(formats strfmt.Registry) error {
	if swag.IsZero(m.Statuses) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *FilterV2) validateFilterAction(formats strfmt.Registry) error {
	if swag.IsZero(m.FilterAction) { // not required
		return nil
	}

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

	return nil
}

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

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

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

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

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

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

func (m *FilterV2) contextValidateContext(ctx context.Context, formats strfmt.Registry) error {

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

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

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

	}

	return nil
}

func (m *FilterV2) contextValidateKeywords(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Keywords[i] != nil {

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

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

	}

	return nil
}

func (m *FilterV2) contextValidateStatuses(ctx context.Context, formats strfmt.Registry) error {

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

		if m.Statuses[i] != nil {

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

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

	}

	return nil
}

func (m *FilterV2) contextValidateFilterAction(ctx context.Context, formats strfmt.Registry) error {

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

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

	return nil
}

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

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