func UniqueFieldValidator(field string, zero interface{}, filters ...string) *Callback {
	return C("fire/UniqueFieldValidator", Only(Create, Update), func(ctx *Context) error {
		// check if field has changed
		if ctx.Operation == Update {
			// get original model
			original, err := ctx.Original()
			if err != nil {
				return err
			}

			// return if field has not been changed
			if reflect.DeepEqual(ctx.Model.MustGet(field), original.MustGet(field)) {
				return nil
			}
		}

		// get value
		value := ctx.Model.MustGet(field)

		// return if value is the zero value
		if value != NoZero && reflect.DeepEqual(value, zero) {
			return nil
		}

		// prepare query
		query := bson.M{
			coal.F(ctx.Model, field): value,
		}

		// add filters
		for _, field := range filters {
			query[coal.F(ctx.Model, field)] = ctx.Model.MustGet(field)
		}

		// exclude soft deleted documents if supported
		if sdm := coal.L(ctx.Model, "fire-soft-delete", false); sdm != "" {
			query[coal.F(ctx.Model, sdm)] = nil
		}

		// count
		ctx.Tracer.Push("mgo/Query.Count")
		ctx.Tracer.Tag("query", query)
		n, err := ctx.Store.C(ctx.Model).Find(query).Limit(1).Count()
		if err != nil {
			return err
		} else if n != 0 {
			return E("attribute %s is not unique", field)
		}
		ctx.Tracer.Pop()

		return nil
	})
}
func UniqueFieldValidator(field string, zero interface{}, filters ...string) *Callback {
	return C("fire/UniqueFieldValidator", Only(Create, Update), func(ctx *Context) error {
		
		if ctx.Operation == Update {
			
			original, err := ctx.Original()
			if err != nil {
				return err
			}

			
			if reflect.DeepEqual(ctx.Model.MustGet(field), original.MustGet(field)) {
				return nil
			}
		}

		
		value := ctx.Model.MustGet(field)

		
		if value != NoZero && reflect.DeepEqual(value, zero) {
			return nil
		}

		
		query := bson.M{
			coal.F(ctx.Model, field): value,
		}

		
		for _, field := range filters {
			query[coal.F(ctx.Model, field)] = ctx.Model.MustGet(field)
		}

		
		if sdm := coal.L(ctx.Model, "fire-soft-delete", false); sdm != "" {
			query[coal.F(ctx.Model, sdm)] = nil
		}

		
		ctx.Tracer.Push("mgo/Query.Count")
		ctx.Tracer.Tag("query", query)
		n, err := ctx.Store.C(ctx.Model).Find(query).Limit(1).Count()
		if err != nil {
			return err
		} else if n != 0 {
			return E("attribute %s is not unique", field)
		}
		ctx.Tracer.Pop()

		return nil
	})
}
func UniqueFieldValidator ( field string , zero interface { } , filters ... string ) * Callback { return C ( " " , Only ( Create , Update ) , func ( ctx * Context ) error { // check if field has changed if ctx . Operation == Update { // get original model original , err := ctx . Original ( ) if err != nil { return err } // return if field has not been changed if reflect . DeepEqual ( ctx . Model . MustGet ( field ) , original . MustGet ( field ) ) { return nil } } // get value value := ctx . Model . MustGet ( field ) // return if value is the zero value if value != NoZero && reflect . DeepEqual ( value , zero ) { return nil } // prepare query query := bson . M { coal . F ( ctx . Model , field ) : value , } // add filters for _ , field := range filters { query [ coal . F ( ctx . Model , field ) ] = ctx . Model . MustGet ( field ) } // exclude soft deleted documents if supported if sdm := coal . L ( ctx . Model , " " , false ) ; sdm != " " { query [ coal . F ( ctx . Model , sdm ) ] = nil } // count ctx . Tracer . Push ( " " ) ctx . Tracer . Tag ( " " , query ) n , err := ctx . Store . C ( ctx . Model ) . Find ( query ) . Limit ( 1 ) . Count ( ) if err != nil { return err } else if n != 0 { return E ( " " , field ) } ctx . Tracer . Pop ( ) return nil } ) }
// UniqueFieldValidator ensures that the specified field of the current model will
// remain unique among the specified filters. If the value matches the provided
// zero value the check is skipped.
//
//	fire.UniqueFieldValidator("Name", "", "Creator")
//
// The special NoZero value can be provided to skip the zero check.
//
// The callback supports models that use the soft delete mechanism.
UniqueFieldValidator ensures that the specified field of the current model will remain unique among the specified filters . If the value matches the provided zero value the check is skipped . fire . UniqueFieldValidator ( Name Creator ) The special NoZero value can be provided to skip the zero check . The callback supports models that use the soft delete mechanism .