func()

in internal/fields/mappings.go [428:506]


func (v *MappingValidator) compareMappings(path string, couldBeParametersDefinition bool, preview, actual map[string]any, dynamicTemplates []map[string]any) multierror.Error {
	var errs multierror.Error

	isConstantKeywordType, err := validateConstantKeywordField(path, preview, actual)
	if err != nil {
		return multierror.Error{err}
	}
	if isConstantKeywordType {
		return nil
	}

	if slices.Contains(v.exceptionFields, path) {
		logger.Tracef("Found exception field, skip its validation: %q", path)
		return nil
	}

	if isObjectFullyDynamic(actual) {
		logger.Warnf("Dynamic object found but no fields ingested under path: \"%s.*\"", path)
		return nil
	}

	// Ensure to validate properties from an object (subfields) in the right location of the mappings
	// there could be "sub-fields" with name "properties" too
	if couldBeParametersDefinition && isObject(actual) {
		if isObjectFullyDynamic(preview) {
			dynamicErrors := v.validateMappingsNotInPreview(path, actual, dynamicTemplates)
			errs = append(errs, dynamicErrors...)
			if len(errs) > 0 {
				return errs.Unique()
			}
			return nil
		} else if !isObject(preview) {
			errs = append(errs, fmt.Errorf("undefined field mappings found in path: %q", path))
			return errs.Unique()
		}
		previewProperties, err := getMappingDefinitionsField("properties", preview)
		if err != nil {
			errs = append(errs, fmt.Errorf("found invalid properties type in preview mappings for path %q: %w", path, err))
		}
		actualProperties, err := getMappingDefinitionsField("properties", actual)
		if err != nil {
			errs = append(errs, fmt.Errorf("found invalid properties type in actual mappings for path %q: %w", path, err))
		}
		compareErrors := v.compareMappings(path, false, previewProperties, actualProperties, dynamicTemplates)
		errs = append(errs, compareErrors...)

		if len(errs) == 0 {
			return nil
		}
		return errs.Unique()
	}

	containsMultifield := isMultiFields(actual)
	if containsMultifield {
		if !isMultiFields(preview) {
			errs = append(errs, fmt.Errorf("field %q is undefined: not found multi_fields definitions in preview mapping", path))
			return errs.Unique()
		}
		previewFields, err := getMappingDefinitionsField("fields", preview)
		if err != nil {
			errs = append(errs, fmt.Errorf("found invalid multi_fields type in preview mappings for path %q: %w", path, err))
		}
		actualFields, err := getMappingDefinitionsField("fields", actual)
		if err != nil {
			errs = append(errs, fmt.Errorf("found invalid multi_fields type in actual mappings for path %q: %w", path, err))
		}
		compareErrors := v.compareMappings(path, false, previewFields, actualFields, dynamicTemplates)
		errs = append(errs, compareErrors...)
		// not returning here to keep validating the other fields of this object if any
	}

	// Compare and validate the elements under "properties": objects or fields and its parameters
	propertiesErrs := v.validateObjectProperties(path, false, containsMultifield, preview, actual, dynamicTemplates)
	errs = append(errs, propertiesErrs...)
	if len(errs) == 0 {
		return nil
	}
	return errs.Unique()
}