Skip to content

Commit 8f36c18

Browse files
committed
Validate fully dynamic objects (preview) with dynamic templates
1 parent 019ffd4 commit 8f36c18

File tree

3 files changed

+36
-51
lines changed

3 files changed

+36
-51
lines changed

internal/fields/dynamic_template.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@ func (d *dynamicTemplate) Matches(currentPath string, definition map[string]any)
3030
name := fieldNameFromPath(currentPath)
3131
if !slices.Contains(d.match, name) {
3232
// If there is no an exact match, it is compared with patterns/wildcards
33-
34-
// logger.Warnf(">>>> no contained %s: %s", d.match, name)
3533
matches, err := stringMatchesPatterns(d.match, name, fullRegex)
3634
if err != nil {
37-
return false, fmt.Errorf("failed to parse dynamic template %s: %w", d.name, err)
35+
return false, fmt.Errorf("failed to parse dynamic template %q: %w", d.name, err)
3836
}
3937

4038
if !matches {
41-
// logger.Debugf(">> Issue match: not matches")
39+
// logger.Debugf(">> Issue matchi %q: not matches", d.name)
4240
return false, nil
4341
}
4442
}
@@ -52,11 +50,11 @@ func (d *dynamicTemplate) Matches(currentPath string, definition map[string]any)
5250

5351
matches, err := stringMatchesPatterns(d.unmatch, name, fullRegex)
5452
if err != nil {
55-
return false, fmt.Errorf("failed to parse dynamic template %s: %w", d.name, err)
53+
return false, fmt.Errorf("failed to parse dynamic template %q: %w", d.name, err)
5654
}
5755

5856
if matches {
59-
// logger.Debugf(">> Issue unmatch: matches")
57+
// logger.Debugf(">> Issue unmatchi %q: matches", d.name)
6058
return false, nil
6159
}
6260
}
@@ -68,18 +66,18 @@ func (d *dynamicTemplate) Matches(currentPath string, definition map[string]any)
6866
return false, fmt.Errorf("failed to parse dynamic template %s: %w", d.name, err)
6967
}
7068
if !matches {
71-
logger.Debugf(">> Issue path_match: not matches (currentPath %s)", currentPath)
69+
// logger.Debugf(">> Issue path_match %q: not matches (currentPath %s)", d.name, currentPath)
7270
return false, nil
7371
}
7472
}
7573

7674
if len(d.unpathMatch) > 0 {
7775
matches, err := stringMatchesPatterns(d.unpathMatch, currentPath, fullRegex)
7876
if err != nil {
79-
return false, fmt.Errorf("failed to parse dynamic template %s: %w", d.name, err)
77+
return false, fmt.Errorf("failed to parse dynamic template %q: %w", d.name, err)
8078
}
8179
if matches {
82-
// logger.Debugf(">> Issue unpath_match: matches")
80+
// logger.Debugf(">> Issue unpath_matchi %q: matches", d.name)
8381
return false, nil
8482
}
8583
}

internal/fields/mappings.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,11 @@ func (v *MappingValidator) compareMappings(path string, couldBeParametersDefinit
450450
// there could be "sub-fields" with name "properties" too
451451
if couldBeParametersDefinition && isObject(actual) {
452452
if isObjectFullyDynamic(preview) {
453-
// TODO: Skip for now, it should be required to compare with dynamic templates
454-
logger.Debugf("Pending to validate with the dynamic templates defined the path: %q", path)
453+
dynamicErrors := v.validateMappingsNotInPreview(path, actual, dynamicTemplates)
454+
errs = append(errs, dynamicErrors...)
455+
if len(errs) > 0 {
456+
return errs.Unique()
457+
}
455458
return nil
456459
} else if !isObject(preview) {
457460
errs = append(errs, fmt.Errorf("not found properties in preview mappings for path: %q", path))

internal/fields/mappings_test.go

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func TestComparingMappings(t *testing.T) {
437437
},
438438
},
439439
{
440-
title: "skip dynamic objects", // TODO: should this be checked using dynamic templates?
440+
title: "validate fully dynamic objects in preview",
441441
preview: map[string]any{
442442
"@timestamp": map[string]any{
443443
"type": "keyword",
@@ -451,6 +451,13 @@ func TestComparingMappings(t *testing.T) {
451451
"type": "object",
452452
"dynamic": "true",
453453
},
454+
"string": map[string]any{
455+
"type": "object",
456+
"dynamic": "true",
457+
},
458+
"foo": map[string]any{
459+
"type": "keyword",
460+
},
454461
},
455462
},
456463
},
@@ -473,61 +480,38 @@ func TestComparingMappings(t *testing.T) {
473480
},
474481
},
475482
},
476-
},
477-
},
478-
},
479-
},
480-
},
481-
schema: []FieldDefinition{},
482-
expectedErrors: []string{},
483-
},
484-
{
485-
title: "compare all objects even dynamic true", // TODO: should this be checked using dynamic templates?
486-
preview: map[string]any{
487-
"@timestamp": map[string]any{
488-
"type": "keyword",
489-
},
490-
"sql": map[string]any{
491-
"properties": map[string]any{
492-
"metrics": map[string]any{
493-
"properties": map[string]any{
494-
"dynamic": "true",
495-
"numeric": map[string]any{
496-
"type": "object",
497-
"dynamic": "true",
498-
},
499-
},
500-
},
501-
},
502-
},
503-
},
504-
actual: map[string]any{
505-
"@timestamp": map[string]any{
506-
"type": "keyword",
507-
},
508-
"sql": map[string]any{
509-
"properties": map[string]any{
510-
"metrics": map[string]any{
511-
"properties": map[string]any{
512-
"dynamic": "true",
513-
"numeric": map[string]any{
483+
"string": map[string]any{
514484
"dynamic": "true",
515485
"properties": map[string]any{
516486
"innodb_data_fsyncs": map[string]any{
517-
"type": "long",
487+
"type": "keyword",
518488
},
519489
},
520490
},
521491
"example": map[string]any{
522492
"type": "keyword",
523493
},
494+
"foo": map[string]any{
495+
"type": "keyword",
496+
},
524497
},
525498
},
526499
},
527500
},
528501
},
502+
dynamicTemplates: []map[string]any{
503+
{
504+
"sql.metrics.string.*": map[string]any{
505+
"path_match": "sql.metrics.string.*",
506+
"mapping": map[string]any{
507+
"type": "keyword",
508+
},
509+
},
510+
},
511+
},
529512
schema: []FieldDefinition{},
530513
expectedErrors: []string{
514+
`field "sql.metrics.numeric.innodb_data_fsyncs" is undefined: field definition not found`,
531515
`field "sql.metrics.example" is undefined: field definition not found`,
532516
},
533517
},

0 commit comments

Comments
 (0)