Sha256: fa73e7db1c4e1ff7e1b321d1feea916279f74a3a382f850cf65cf0d915ce3a00

Contents?: true

Size: 833 Bytes

Versions: 14

Compression:

Stored size: 833 Bytes

Contents

<%-
import 'context'
import 'fmt'
import model.package

validation_error = packages.type_for('ValidationError')
raise "ValidationError not found" unless validation_error
import validation_error.package
-%>

func (s *<%= type.name %>) ValidateUniqueness(ctx context.Context, m *<%= model.qualified_name %>) error {
	conditions := map[string]interface{}{
<%- model.fields.select(&:unique).each do |field| -%>
		"<%= field.name %>": m.<%= field.name %>,
<%- end -%>
  }
	for field, value := range conditions {
		q := s.Query().Filter(field+" =", value)
		c, err := s.CountBy(ctx, q)
		if err != nil {
			return err
		}
		b := 0
		if m.IsPersisted() {
			b = 1
		}
		if c > b {
			return &<%= validation_error.qualified_name %>{
				Field:   field,
				Message: fmt.Sprintf("%v has already been taken", value),
			}
		}
	}
	return nil
}

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
goon_model_gen-0.4.3 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.4.2 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.4.1 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.4.0 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.3.1 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.3.0 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.2.5 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.2.4 lib/goon_model_gen/templates/store/goon/23_ValidateUniqueness.go.erb
goon_model_gen-0.2.3 lib/goon_model_gen/templates/store/goon/22_ValidateUniqueness.go.erb
goon_model_gen-0.2.2 lib/goon_model_gen/templates/store/goon/22_ValidateUniqueness.go.erb
goon_model_gen-0.2.1 lib/goon_model_gen/templates/store/goon/22_ValidateUniqueness.go.erb
goon_model_gen-0.2.0 lib/goon_model_gen/templates/store/goon/22_ValidateUniqueness.go.erb
goon_model_gen-0.1.3 lib/goon_model_gen/templates/store/goon/22_ValidateUniqueness.go.erb
goon_model_gen-0.1.2 lib/goon_model_gen/templates/store/goon/22_ValidateUniqueness.go.erb