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

// OpenapiTableSchema TableSchema
//
// TableSchema is the schema definition of a single table.
//
// swagger:model openapiTableSchema
type OpenapiTableSchema struct {

	// The column definition for each column in the table.
	// Example: [{"column_name":"id","column_type":"INTEGER"},{"column_name":"column01","column_type":"VARCHAR(255)"}]
	// Required: true
	ColumnDefinitions []*OpenapiTableSchemaColumnDefinitionsItems0 `json:"column_definitions"`

	// The primary key column names for the table. This is optional. The primary key is taken into account when the table is pre-created before an import task is started.
	// Example: ["id"]
	PrimaryKeyColumns []string `json:"primary_key_columns"`
}

// Validate validates this openapi table schema
func (m *OpenapiTableSchema) Validate(formats strfmt.Registry) error {
	var res []error

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

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

func (m *OpenapiTableSchema) validateColumnDefinitions(formats strfmt.Registry) error {

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

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

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

	}

	return nil
}

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

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

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

func (m *OpenapiTableSchema) contextValidateColumnDefinitions(ctx context.Context, formats strfmt.Registry) error {

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

		if m.ColumnDefinitions[i] != nil {

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

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

	}

	return nil
}

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

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

// OpenapiTableSchemaColumnDefinitionsItems0 ColumnDefinition
//
// ColumnDefinition is the definition of a column in a table.
//
// swagger:model OpenapiTableSchemaColumnDefinitionsItems0
type OpenapiTableSchemaColumnDefinitionsItems0 struct {

	// The column name.
	// Example: column01
	// Required: true
	ColumnName *string `json:"column_name"`

	// The column type.
	// Example: VARCHAR(255)
	// Required: true
	ColumnType *string `json:"column_type"`
}

// Validate validates this openapi table schema column definitions items0
func (m *OpenapiTableSchemaColumnDefinitionsItems0) Validate(formats strfmt.Registry) error {
	var res []error

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

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

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

func (m *OpenapiTableSchemaColumnDefinitionsItems0) validateColumnName(formats strfmt.Registry) error {

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

	return nil
}

func (m *OpenapiTableSchemaColumnDefinitionsItems0) validateColumnType(formats strfmt.Registry) error {

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

	return nil
}

// ContextValidate validates this openapi table schema column definitions items0 based on context it is used
func (m *OpenapiTableSchemaColumnDefinitionsItems0) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	return nil
}

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

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