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