You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: document_validation.go
    path of file: ./repos/FerretDB/internal/types
    
    the code of the file until where you have to start completion: // Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package types

import (
	"errors"
	"fmt"
	"math"
	"strings"
	"unicode/utf8"

	"github.com/FerretDB/FerretDB/internal/util/must"
)

//go:generate ../../bin/stringer -linecomment -type ValidationErrorCode

// ValidationErrorCode represents ValidationData error code.
type ValidationErrorCode int

const (
	_ ValidationErrorCode = iota

	// ErrValidation indicates that document is invalid.
	ErrValidation

	// ErrWrongIDType indicates that _id field is invalid.
	ErrWrongIDType

	// ErrIDNotFound indicates that _id field is not found.
	ErrIDNotFound
)

// 
    
func (d *Document) validateData(isTopLevel bool) error {
	d.moveIDToTheFirstIndex()

	keys := d.Keys()
	values := d.Values()

	duplicateChecker := make(map[string]struct{}, len(keys))
	var idPresent bool

	for i, key := range keys {
		// integration tests for those cases are in the `dance` repo

		if !utf8.ValidString(key) {
			return newValidationError(ErrValidation, fmt.Errorf("invalid key: %q (not a valid UTF-8 string)", key))
		}

		if strings.HasPrefix(key, "$") {
			return newValidationError(ErrValidation, fmt.Errorf("invalid key: %q (key must not start with '$' sign)", key))
		}

		if strings.Contains(key, ".") {
			return newValidationError(ErrValidation, fmt.Errorf("invalid key: %q (key must not contain '.' sign)", key))
		}

		if _, ok := duplicateChecker[key]; ok {
			return newValidationError(ErrValidation, fmt.Errorf("invalid key: %q (duplicate keys are not allowed)", key))
		}
		duplicateChecker[key] = struct{}{}

		if key == "_id" {
			idPresent = true
		}

		value := values[i]

		switch value := value.(type) {
		case *Document:
			err := value.validateData(false)
			if err != nil {
				var vErr *ValidationError

				if errors.As(err, &vErr) && vErr.code == ErrIDNotFound {
					continue
				}

				return err
			}
		case *Array:
			if isTopLevel && key == "_id" {
				return newValidationError(ErrWrongIDType, fmt.Errorf("The '_id' value cannot be of type array"))
			}

			for i := 0; i < value.Len(); i++ {
				item := must.NotFail(value.Get(i))

				switch item := item.(type) {
				case *Document:
					err := item.validateData(false)
					if err != nil {
						var vErr *ValidationError

						if errors.As(err, &vErr) && vErr.code == ErrIDNotFound {
							continue
						}

						return err
					}
				case *Array:
					return newValidationError(ErrValidation, fmt.Errorf(
						"invalid value: { %q: %v } (nested arrays are not supported)", key, FormatAnyValue(value),
					))
				case float64:
					if item == 0 && math.Signbit(item) {
						must.NoError(value.Set(i, math.Copysign(0, +1)))
					}
				}
			}
		case float64:
			if math.IsInf(value, 0) {
				return newValidationError(
					ErrValidation, fmt.Errorf("invalid value: { %q: %f } (infinity values are not allowed)", key, value),
				)
			}

			if value == 0 && math.Signbit(value) {
				d.Set(key, math.Copysign(0, +1))
			}
		case Regex:
			if isTopLevel && key == "_id" {
				return newValidationError(ErrWrongIDType, fmt.Errorf("The '_id' value cannot be of type regex"))
			}
		}
	}

	if !idPresent {
		return newValidationError(ErrIDNotFound, fmt.Errorf("invalid document: document must contain '_id' field"))
	}

	return nil
}