@@ -37,9 +37,13 @@ const (
3737	defaultSpecExt  =  ".yaml" 
3838)
3939
40+ type  validator  interface  {
41+ 	Validate (* cdi.Spec ) error 
42+ }
43+ 
4044var  (
4145	// Externally set CDI Spec validation function. 
42- 	specValidator  func ( * cdi. Spec )  error 
46+ 	specValidator  validator 
4347	validatorLock  sync.RWMutex 
4448)
4549
@@ -236,6 +240,9 @@ func (s *Spec) validate() (map[string]*Device, error) {
236240		}
237241		devices [d .Name ] =  dev 
238242	}
243+ 	if  len (devices ) ==  0  {
244+ 		return  nil , fmt .Errorf ("invalid spec, no devices" )
245+ 	}
239246
240247	return  devices , nil 
241248}
@@ -253,10 +260,10 @@ func ParseSpec(data []byte) (*cdi.Spec, error) {
253260// SetSpecValidator sets a CDI Spec validator function. This function 
254261// is used for extra CDI Spec content validation whenever a Spec file 
255262// loaded (using ReadSpec() or written (using WriteSpec()). 
256- func  SetSpecValidator (fn   func ( * cdi. Spec )  error ) {
263+ func  SetSpecValidator (v   validator ) {
257264	validatorLock .Lock ()
258265	defer  validatorLock .Unlock ()
259- 	specValidator  =  fn 
266+ 	specValidator  =  v 
260267}
261268
262269// validateSpec validates the Spec using the external validator. 
@@ -267,7 +274,7 @@ func validateSpec(raw *cdi.Spec) error {
267274	if  specValidator  ==  nil  {
268275		return  nil 
269276	}
270- 	err  :=  specValidator (raw )
277+ 	err  :=  specValidator . Validate (raw )
271278	if  err  !=  nil  {
272279		return  fmt .Errorf ("Spec validation failed: %w" , err )
273280	}
0 commit comments