ardanlabs/conf

Problems with required

BrunoPereira opened this issue · 3 comments

Hi

There's a problem with the required field.

`

	var provided bool
	for _, sourcer := range sources {
		if sourcer == nil  {
			continue
		}
		var value string
		if value, provided = sourcer.Source(field); !provided {
			continue
		}

		// A value was found so update the struct value with it.
		if err := processField(value, field.field); err != nil {
			return &FieldError{
				fieldName: field.name,
				typeName:  field.field.Type().String(),
				value:     value,
				err:       err,
			}
		}
	}

	// If this key is not provided by any source, check if it was
	// required to be provided.
	if !provided && field.options.required {
		return fmt.Errorf("required field %s is missing value", field.name)
	}`

Since there are multiple sources, the required field could have been in the 1st source the for loop processes.
No problem here.
But when you process the 2nd (..or any other), the "provided flag" is marked as false.

So although the field was processed with success 1 time, the final condition will still return an error.

I fix this in the code bellow (conf.go, line 82)
for _, sourcer := range sources { if sourcer == nil || provided { continue }

Would you consider providing this fix in a PR with a test case?

Sure ....
Just need to make a new branch for it... the fork I have already has a fix for the issue#1 that I reported

@ardan-bkennedy Since this was my 1st pull request, please let me know if I should have done something different.
tnx