@@ -493,4 +493,118 @@ describe('serialization/record', function () {
493493 assert . strictEqual ( parsed . choice , false )
494494 } )
495495 } )
496+
497+ describe ( 'numeric range and bounds validation' , function ( ) {
498+ defineAlias ( 'test.record.JsUint' , {
499+ primitive : 'integer' ,
500+ min : 0 ,
501+ max : 9007199254740991 ,
502+ } )
503+
504+ const LocateNodesParameters = defineRecord ( 'test.record.LocateNodesParameters' , [
505+ { name : 'maxNodeCount' , wire : 'maxNodeCount' , required : false , type : { ref : 'test.record.JsUint' , min : 1 } } ,
506+ ] )
507+
508+ const ResponseData = defineRecord ( 'test.record.ResponseData' , [
509+ { name : 'status' , wire : 'status' , required : true , type : { ref : 'test.record.JsUint' } } ,
510+ ] )
511+
512+ const ViewportParameters = defineRecord ( 'test.record.ViewportParameters' , [
513+ {
514+ name : 'devicePixelRatio' ,
515+ wire : 'devicePixelRatio' ,
516+ required : false ,
517+ type : { primitive : 'number' , min : 0 , exclusiveMin : true , nullable : true } ,
518+ } ,
519+ ] )
520+
521+ const GeolocationCoordinates = defineRecord ( 'test.record.GeolocationCoordinates' , [
522+ {
523+ name : 'heading' ,
524+ wire : 'heading' ,
525+ required : false ,
526+ type : { primitive : 'number' , min : 0 , max : 360 , exclusiveMax : true , nullable : true } ,
527+ } ,
528+ ] )
529+
530+ const PrintParameters = defineRecord ( 'test.record.PrintParameters' , [
531+ {
532+ name : 'pageRanges' ,
533+ wire : 'pageRanges' ,
534+ required : false ,
535+ type : { list : { union : [ { ref : 'test.record.JsUint' } , { primitive : 'string' } ] } } ,
536+ } ,
537+ ] )
538+
539+ it ( 'holds an alias-typed field to its declared range outbound' , function ( ) {
540+ const resp = new ResponseData ( { status : 200 } )
541+ assert . strictEqual ( resp . status , 200 )
542+
543+ assert . throws ( ( ) => new ResponseData ( { status : - 1 } ) , / e x p e c t e d > = 0 , g o t - 1 / )
544+ assert . throws ( ( ) => new ResponseData ( { status : 9007199254740992 } ) , / e x p e c t e d < = 9 0 0 7 1 9 9 2 5 4 7 4 0 9 9 1 / )
545+ } )
546+
547+ it ( 'rejects a value outside the range of the alias it names on receipt' , function ( ) {
548+ const parsed = ResponseData . fromWire ( { status : 200 } )
549+ assert . strictEqual ( parsed . status , 200 )
550+
551+ assert . throws ( ( ) => ResponseData . fromWire ( { status : - 1 } ) , / e x p e c t e d > = 0 , g o t - 1 / )
552+ assert . throws ( ( ) => ResponseData . fromWire ( { status : 9007199254740992 } ) , / e x p e c t e d < = 9 0 0 7 1 9 9 2 5 4 7 4 0 9 9 1 / )
553+ } )
554+
555+ it ( 'holds a field narrowing an alias to its tightened range (e.g. maxNodeCount 0 vs 1)' , function ( ) {
556+ const valid = new LocateNodesParameters ( { maxNodeCount : 1 } )
557+ assert . strictEqual ( valid . maxNodeCount , 1 )
558+
559+ const parsed = LocateNodesParameters . fromWire ( { maxNodeCount : 1 } )
560+ assert . strictEqual ( parsed . maxNodeCount , 1 )
561+
562+ assert . throws ( ( ) => new LocateNodesParameters ( { maxNodeCount : 0 } ) , / e x p e c t e d > = 1 , g o t 0 / )
563+ assert . throws ( ( ) => LocateNodesParameters . fromWire ( { maxNodeCount : 0 } ) , / e x p e c t e d > = 1 , g o t 0 / )
564+
565+ assert . throws ( ( ) => new LocateNodesParameters ( { maxNodeCount : - 1 } ) , / e x p e c t e d > = 0 , g o t - 1 / )
566+ assert . throws ( ( ) => LocateNodesParameters . fromWire ( { maxNodeCount : - 1 } ) , / e x p e c t e d > = 0 , g o t - 1 / )
567+ } )
568+
569+ it ( 'enforces exclusive min bound on floating point numbers (e.g. devicePixelRatio > 0)' , function ( ) {
570+ const valid = new ViewportParameters ( { devicePixelRatio : 0.5 } )
571+ assert . strictEqual ( valid . devicePixelRatio , 0.5 )
572+
573+ const parsed = ViewportParameters . fromWire ( { devicePixelRatio : 1 } )
574+ assert . strictEqual ( parsed . devicePixelRatio , 1 )
575+
576+ const nullVal = new ViewportParameters ( { devicePixelRatio : null } )
577+ assert . strictEqual ( nullVal . devicePixelRatio , null )
578+
579+ assert . throws ( ( ) => new ViewportParameters ( { devicePixelRatio : 0 } ) , / e x p e c t e d > 0 , g o t 0 / )
580+ assert . throws ( ( ) => ViewportParameters . fromWire ( { devicePixelRatio : 0 } ) , / e x p e c t e d > 0 , g o t 0 / )
581+ assert . throws ( ( ) => new ViewportParameters ( { devicePixelRatio : - 0.1 } ) , / e x p e c t e d > 0 , g o t - 0 .1 / )
582+ } )
583+
584+ it ( 'enforces exclusive max bound on floating point numbers (e.g. heading < 360)' , function ( ) {
585+ const valid = new GeolocationCoordinates ( { heading : 359.9 } )
586+ assert . strictEqual ( valid . heading , 359.9 )
587+
588+ const zero = new GeolocationCoordinates ( { heading : 0 } )
589+ assert . strictEqual ( zero . heading , 0 )
590+
591+ const parsed = GeolocationCoordinates . fromWire ( { heading : 180 } )
592+ assert . strictEqual ( parsed . heading , 180 )
593+
594+ assert . throws ( ( ) => new GeolocationCoordinates ( { heading : 360 } ) , / e x p e c t e d < 3 6 0 , g o t 3 6 0 / )
595+ assert . throws ( ( ) => GeolocationCoordinates . fromWire ( { heading : 360 } ) , / e x p e c t e d < 3 6 0 , g o t 3 6 0 / )
596+ assert . throws ( ( ) => new GeolocationCoordinates ( { heading : - 1 } ) , / e x p e c t e d > = 0 , g o t - 1 / )
597+ } )
598+
599+ it ( 'holds union list elements to their declared numeric ranges (e.g. pageRanges)' , function ( ) {
600+ const valid = new PrintParameters ( { pageRanges : [ '1-3' , 2 ] } )
601+ assert . deepStrictEqual ( valid . pageRanges , [ '1-3' , 2 ] )
602+
603+ const parsed = PrintParameters . fromWire ( { pageRanges : [ '1-3' , 2 ] } )
604+ assert . deepStrictEqual ( parsed . pageRanges , [ '1-3' , 2 ] )
605+
606+ assert . throws ( ( ) => new PrintParameters ( { pageRanges : [ '1-3' , - 1 ] } ) , ValidationError )
607+ assert . throws ( ( ) => PrintParameters . fromWire ( { pageRanges : [ '1-3' , - 1 ] } ) , ValidationError )
608+ } )
609+ } )
496610} )
0 commit comments