<commit_before>package validator

import (
	"errors"
	"fmt"
	"reflect"

	"github.com/gocraft/dbr"
)

var (
	RequiredDefinitionEmptyError       = errors.New("the required value should have at least one element")
	RequiredDefinitionDuplicationError = errors.New("the required value should not be duplicated")
)

type RequiredValidator struct {
	definition RequiredValidatorDefinition
}

type RequiredValidatorDefinition struct {
	Required []string `json:"pattern"`
}

type RequiredValidationError struct {
	Input      interface{}                 `json:"input"`
	Definition RequiredValidatorDefinition `json:"definition"`
}

func (r RequiredValidationError) Error() string {
	return fmt.Sprintf("input struct does not satisfy required values '%v'\n", r.Definition.Required)
}

func NewRequiredValidator(definition RequiredValidatorDefinition) (RequiredValidator, error) {
	required := definition.Required
	len := len(required)
	if len == 0 {
		return RequiredValidator{}, RequiredDefinitionEmptyError
	}

	for i := 0; i < len-1; i++ {
		key := required[i]
		for j := i + 1; j < len; j++ {
			if required[j] == key {
				return RequiredValidator{}, RequiredDefinitionDuplicationError
			}
		}
	}

	return RequiredValidator{definition}, nil
}

func (r RequiredValidator) Validate(input interface{}) error {
	if input == nil {
		return &InvalidTypeError{
			Definition: r.definition,
			Input:      input,
		}
	}

	var v reflect.Value
	if reflect.TypeOf(input).Kind() != reflect.Ptr {
		v = reflect.ValueOf(input)
	} else {
		v = reflect.ValueOf(input).Elem()
	}
	if v.Kind() != reflect.Struct {
		return &InvalidTypeError{
			Definition: r.definition,
			Input:      input,
		}
	}

	for _, key := range r.definition.Required {
		e := v.FieldByName(key)
		if e.Kind() == reflect.Ptr {
			if e.IsNil() {
				return &RequiredValidationError{<commit_msg>Check if field exists<commit_after>package validator

import (
	"errors"
	"fmt"
	"reflect"

	"github.com/gocraft/dbr"
)

var (
	RequiredDefinitionEmptyError       = errors.New("the required value should have at least one element")
	RequiredDefinitionDuplicationError = errors.New("the required value should not be duplicated")
)

type RequiredValidator struct {
	definition RequiredValidatorDefinition
}

type RequiredValidatorDefinition struct {
	Required []string `json:"pattern"`
}

type RequiredValidationError struct {
	Input      interface{}                 `json:"input"`
	Definition RequiredValidatorDefinition `json:"definition"`
}

func (r RequiredValidationError) Error() string {
	return fmt.Sprintf("input struct does not satisfy required values '%v'\n", r.Definition.Required)
}

func NewRequiredValidator(definition RequiredValidatorDefinition) (RequiredValidator, error) {
	required := definition.Required
	len := len(required)
	if len == 0 {
		return RequiredValidator{}, RequiredDefinitionEmptyError
	}

	for i := 0; i < len-1; i++ {
		key := required[i]
		for j := i + 1; j < len; j++ {
			if required[j] == key {
				return RequiredValidator{}, RequiredDefinitionDuplicationError
			}
		}
	}

	return RequiredValidator{definition}, nil
}

func (r RequiredValidator) Validate(input interface{}) error {
	if input == nil {
		return &InvalidTypeError{
			Definition: r.definition,
			Input:      input,
		}
	}

	var v reflect.Value
	if reflect.TypeOf(input).Kind() != reflect.Ptr {
		v = reflect.ValueOf(input)
	} else {
		v = reflect.ValueOf(input).Elem()
	}
	if v.Kind() != reflect.Struct {
		return &InvalidTypeError{
			Definition: r.definition,
			Input:      input,
		}
	}

	for _, key := range r.definition.Required {
		e := v.FieldByName(key)
		if (e == reflect.Value{}) {
			return &InvalidFieldTypeError{
				Definition: r.definition,
				Input:      input,
			}
		}

		if e.Kind() == reflect.Ptr {
			if e.IsNil() {
				return &RequiredValidationError{
{'fraction_non_alphanumeric': 0.11633986928104575, 'fraction_numerical': 0.0020915032679738564, 'mean_word_length': 3.9882659713168187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1715984', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1124, 'n_words': 307}