Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,4 @@ validation-defaults:
length: false
range: false
max-elements: false # Not yet implemented
unique: false
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ require (
github.com/scrapli/scrapligo v1.4.0
github.com/sdcio/cache v0.0.38
github.com/sdcio/logger v0.0.3
github.com/sdcio/schema-server v0.0.34
github.com/sdcio/sdc-protos v0.0.54
github.com/sdcio/schema-server v0.0.34-0.20260605130536-a10329fec3d6
github.com/sdcio/sdc-protos v0.0.55-0.20260603141304-280945df8b0f
github.com/sdcio/yang-parser v0.0.12
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ github.com/sdcio/goyang v1.6.2-2 h1:qfeUKBmoKpiKAruuEc3+V8wgHKP/n1jRDEnTy23knV8=
github.com/sdcio/goyang v1.6.2-2/go.mod h1:5WolITjek1NF8yrNERyVZ7jqjOClJTpO8p/+OwmETM4=
github.com/sdcio/logger v0.0.3 h1:IFUbObObGry+S8lHGwOQKKRxJSuOphgRU/hxVhOdMOM=
github.com/sdcio/logger v0.0.3/go.mod h1:yWaOxK/G6vszjg8tKZiMqiEjlZouHsjFME4zSk+SAEA=
github.com/sdcio/schema-server v0.0.34 h1:NNDOkvtUMONtBA7cVvN96F+FWGD/Do6HNqfchy9B8eI=
github.com/sdcio/schema-server v0.0.34/go.mod h1:6t8HLXpqUqEJmE5yNZh29u/KZw0jlOICdNWns7zE4GE=
github.com/sdcio/sdc-protos v0.0.54 h1:1EbtU9ZbbJHFPOFGi5aW8Th79cuY9i+AJaP0ABVx8hw=
github.com/sdcio/sdc-protos v0.0.54/go.mod h1:YMLHbey0/aL1qtLW8csSYVPafsgnnn7aY54HkV5dbyQ=
github.com/sdcio/schema-server v0.0.34-0.20260605130536-a10329fec3d6 h1:byYQVpakvE+yZVLWWTjXBnF2nPdSM+GPb37CXrwoiRQ=
github.com/sdcio/schema-server v0.0.34-0.20260605130536-a10329fec3d6/go.mod h1:gCEg7uCzSmZ5altprltV7LT+QFPtPFPMkHA93YHXIc4=
github.com/sdcio/sdc-protos v0.0.55-0.20260603141304-280945df8b0f h1:8nJP7/M4rT2AxHHTY8DZHQjadHdoUvWKVnV4b+tvr48=
github.com/sdcio/sdc-protos v0.0.55-0.20260603141304-280945df8b0f/go.mod h1:NsvzvHnTonLcwQ/WNzxzBCauQmqxpuviaW0wh7Lkrts=
github.com/sdcio/yang-parser v0.0.12 h1:RSSeqfAOIsJx5Lno5u4/ezyOmQYUduQ22rBfU/mtpJ4=
github.com/sdcio/yang-parser v0.0.12/go.mod h1:CBqn3Miq85qmFVGHxHXHLluXkaIOsTzV06IM4DW6+D4=
github.com/sirikothe/gotextfsm v1.0.1-0.20200816110946-6aa2cfd355e4 h1:FHUL2HofYJuslFOQdy/JjjP36zxqIpd/dcoiwLMIs7k=
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Validators struct {
Length bool `yaml:"length,omitempty" json:"length,omitempty"`
Range bool `yaml:"range,omitempty" json:"range,omitempty"`
MaxElements bool `yaml:"max-elements,omitempty" json:"max-elements,omitempty"`
Unique bool `yaml:"unique,omitempty" json:"unique,omitempty"`
}

func (v *Validators) DisableAll() *Validators {
Expand All @@ -51,6 +52,7 @@ func (v *Validators) DisableAll() *Validators {
v.MustStatement = true
v.Pattern = true
v.Range = true
v.Unique = true
return v
}

Expand All @@ -64,5 +66,6 @@ func (v *Validators) DeepCopy() *Validators {
Length: v.Length,
Range: v.Range,
MaxElements: v.MaxElements,
Unique: v.Unique,
}
}
3 changes: 3 additions & 0 deletions pkg/tree/ops/validation/validation_dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func activeValidators(vCfg *config.Validation) []ValidationFunc {
if !vCfg.DisabledValidators.MaxElements {
active = append(active, validateMinMaxElements)
}
if !vCfg.DisabledValidators.Unique {
active = append(active, validateUnique)
}
return active
}

Expand Down
158 changes: 158 additions & 0 deletions pkg/tree/ops/validation/validation_entry_unique.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package validation

import (
"context"
"fmt"
"strings"

"github.com/sdcio/data-server/pkg/tree/api"
"github.com/sdcio/data-server/pkg/tree/ops"
"github.com/sdcio/data-server/pkg/tree/types"
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
)

func validateUnique(_ context.Context, e api.Entry, resultChan chan<- *types.ValidationResultEntry, stats *types.ValidationStats) {
contSchema := e.GetSchema().GetContainer()
if contSchema == nil {
return
}
constraints := contSchema.GetUniqueConstraints()
if len(constraints) == 0 {
return
}
stats.Add(types.StatTypeUnique, uint32(len(constraints)))

childs, err := ops.GetListChilds(e)
if err != nil {
resultChan <- types.NewValidationResultEntry("unknown", fmt.Errorf("validateUnique: GetListChilds: %w", err), types.ValidationResultEntryTypeError)
return
}

// filter out instances that will not exist after the transaction
surviving := make([]api.Entry, 0, len(childs))
for _, c := range childs {
if c.RemainsToExist() {
surviving = append(surviving, c)
}
}

for _, constraint := range constraints {
elems := constraint.GetElements()

// v1: skip multi-segment paths and warn
hasMultiSeg := false
for _, elem := range elems {
if strings.Contains(elem, "/") {
hasMultiSeg = true
break
}
}
if hasMultiSeg {
resultChan <- types.NewValidationResultEntry(
"unknown",
fmt.Errorf("list %s: unique constraint %v contains multi-segment path — skipped in v1", e.SdcpbPath().ToXPath(false), elems),
types.ValidationResultEntryTypeWarning,
)
continue
}

// Build a TypedValue tuple for each surviving instance.
// An instance whose tuple is incomplete (missing leaf) is excluded per RFC 7950 § 7.8.3.
type instanceTuple struct {
entry api.Entry
values []*sdcpb.TypedValue
owner string
}
tuples := make([]instanceTuple, 0, len(surviving))

for _, inst := range surviving {
leafChilds := inst.GetChilds(types.DescendMethodActiveChilds)
tuple := make([]*sdcpb.TypedValue, 0, len(elems))
complete := true

for _, elemName := range elems {
leafEntry, exists := leafChilds[elemName]
if !exists || leafEntry.GetLeafVariants() == nil {
complete = false
break
}
le := leafEntry.GetLeafVariants().GetHighestPrecedence(false, false, false)
if le == nil {
complete = false
break
}
tv := le.Update.Value()
if tv == nil {
complete = false
break
}
tuple = append(tuple, tv)
}

if !complete {
continue
}

// Derive the owner from the key leaf's highest-precedence variant.
owner := ownerFromKeyLeaf(inst, contSchema)
tuples = append(tuples, instanceTuple{entry: inst, values: tuple, owner: owner})
}

// O(n²) pairwise collision check.
for i := 0; i < len(tuples); i++ {
for j := i + 1; j < len(tuples); j++ {
a, b := tuples[i], tuples[j]
if tuplesEqual(a.values, b.values) {
msg := collisionMessage(e, a.entry, b.entry, elems, a.values)
resultChan <- types.NewValidationResultEntry(a.owner, fmt.Errorf("%s", msg), types.ValidationResultEntryTypeError)
resultChan <- types.NewValidationResultEntry(b.owner, fmt.Errorf("%s", msg), types.ValidationResultEntryTypeError)
}
}
}
}
}

// ownerFromKeyLeaf reads the owner from the first key leaf's highest-precedence variant.
func ownerFromKeyLeaf(inst api.Entry, contSchema *sdcpb.ContainerSchema) string {
keys := contSchema.GetKeys()
if len(keys) == 0 {
return "unknown"
}
leafChilds := inst.GetChilds(types.DescendMethodActiveChilds)
keyLeaf, exists := leafChilds[keys[0].GetName()]
if !exists {
return "unknown"
}
le := keyLeaf.GetLeafVariants().GetHighestPrecedence(false, false, false)
if le == nil {
return "unknown"
}
return le.Update.Owner()
}

func tuplesEqual(a, b []*sdcpb.TypedValue) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if !a[i].Equal(b[i]) {
return false
}
}
return true
}

func collisionMessage(listEntry, a, b api.Entry, elems []string, vals []*sdcpb.TypedValue) string {
valParts := make([]string, 0, len(elems))
for i, elem := range elems {
valParts = append(valParts, fmt.Sprintf("%s=%v", elem, vals[i]))
}
return fmt.Sprintf(
"list %s: unique constraint %v violated — entries %q and %q share the same values (%s)",
listEntry.SdcpbPath().ToXPath(false),
elems,
a.SdcpbPath().ToXPath(false),
b.SdcpbPath().ToXPath(false),
strings.Join(valParts, ", "),
)
}
Loading
Loading