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

package model

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

// CentralAPIKey central Api key
//
// swagger:model centralApiKey
type CentralAPIKey struct {

	// access key
	AccessKey string `json:"access_key,omitempty"`

	// biz attr
	BizAttr string `json:"biz_attr,omitempty"`

	// created at
	// Format: date-time
	CreatedAt strfmt.DateTime `json:"created_at,omitempty"`

	// created by
	CreatedBy string `json:"created_by,omitempty"`

	// description
	Description string `json:"description,omitempty"`

	// expired at
	// Format: date-time
	ExpiredAt strfmt.DateTime `json:"expired_at,omitempty"`

	// id
	ID string `json:"id,omitempty"`

	// resource infos
	ResourceInfos []*CentralAPIKeyResourceInfo `json:"resource_infos"`

	// secret key
	SecretKey string `json:"secret_key,omitempty"`

	// source
	Source string `json:"source,omitempty"`

	// updated at
	// Format: date-time
	UpdatedAt strfmt.DateTime `json:"updated_at,omitempty"`

	// updated by
	UpdatedBy string `json:"updated_by,omitempty"`
}

// Validate validates this central Api key
func (m *CentralAPIKey) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

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

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

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

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

	return nil
}

func (m *CentralAPIKey) validateExpiredAt(formats strfmt.Registry) error {
	if swag.IsZero(m.ExpiredAt) { // not required
		return nil
	}

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

	return nil
}

func (m *CentralAPIKey) validateResourceInfos(formats strfmt.Registry) error {
	if swag.IsZero(m.ResourceInfos) { // not required
		return nil
	}

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

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

	}

	return nil
}

func (m *CentralAPIKey) validateUpdatedAt(formats strfmt.Registry) error {
	if swag.IsZero(m.UpdatedAt) { // not required
		return nil
	}

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

	return nil
}

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

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

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

func (m *CentralAPIKey) contextValidateResourceInfos(ctx context.Context, formats strfmt.Registry) error {

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

		if m.ResourceInfos[i] != nil {

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

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

	}

	return nil
}

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

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