@@ -360,6 +360,66 @@ func TestBuildOpenAPISchema(t *testing.T) {
360
360
want : nil ,
361
361
wantErr : true ,
362
362
},
363
+ {
364
+ name : "Simple string validation" ,
365
+ obj : map [string ]interface {}{
366
+ "name" : `string | validation="self.name != 'invalid'"` ,
367
+ },
368
+ want : & extv1.JSONSchemaProps {
369
+ Type : "object" ,
370
+ Properties : map [string ]extv1.JSONSchemaProps {
371
+ "name" : {
372
+ Type : "string" ,
373
+ XValidations : []extv1.ValidationRule {
374
+ {
375
+ Rule : "self.name != 'invalid'" ,
376
+ Message : "validation failed" ,
377
+ },
378
+ },
379
+ },
380
+ },
381
+ },
382
+ wantErr : false ,
383
+ },
384
+ {
385
+ name : "Multiple field validations" ,
386
+ obj : map [string ]interface {}{
387
+ "age" : `integer | validation="self.age >= 0 && self.age <= 120"` ,
388
+ "name" : `string | validation="self.name.length() >= 3"` ,
389
+ },
390
+ want : & extv1.JSONSchemaProps {
391
+ Type : "object" ,
392
+ Properties : map [string ]extv1.JSONSchemaProps {
393
+ "age" : {
394
+ Type : "integer" ,
395
+ XValidations : []extv1.ValidationRule {
396
+ {
397
+ Rule : "self.age >= 0 && self.age <= 120" ,
398
+ Message : "validation failed" ,
399
+ },
400
+ },
401
+ },
402
+ "name" : {
403
+ Type : "string" ,
404
+ XValidations : []extv1.ValidationRule {
405
+ {
406
+ Rule : "self.name.length() >= 3" ,
407
+ Message : "validation failed" ,
408
+ },
409
+ },
410
+ },
411
+ },
412
+ },
413
+ wantErr : false ,
414
+ },
415
+ {
416
+ name : "Empty validation" ,
417
+ obj : map [string ]interface {}{
418
+ "age" : `integer | validation=""` ,
419
+ },
420
+ want : nil ,
421
+ wantErr : true ,
422
+ },
363
423
}
364
424
365
425
for _ , tt := range tests {
0 commit comments