@@ -3,7 +3,7 @@ import * as module from 'node:module';
33import { describe , expect , it } from 'vitest' ;
44import { z } from 'zod' ;
55
6- import { $BooleanLike , $NumberLike , $UrlLike , isZodType } from '../zod.js' ;
6+ import { $BooleanLike , $NumberLike , $Uint8ArrayLike , $ UrlLike, isZodType } from '../zod.js' ;
77
88const require = module . createRequire ( import . meta. url ) ;
99
@@ -84,3 +84,35 @@ describe('$UrlLike', () => {
8484 expect ( result . data ) . toBeInstanceOf ( URL ) ;
8585 } ) ;
8686} ) ;
87+
88+ describe ( '$Uint8ArrayLike' , ( ) => {
89+ it ( 'should pass when given a Uint8Array' , ( ) => {
90+ const input = new Uint8Array ( [ 1 , 2 , 3 ] ) ;
91+ const result = $Uint8ArrayLike . parse ( input ) ;
92+ expect ( result ) . toBeInstanceOf ( Uint8Array ) ;
93+ expect ( [ ...result ] ) . toEqual ( [ 1 , 2 , 3 ] ) ;
94+ } ) ;
95+
96+ it ( 'should convert an array to Uint8Array' , ( ) => {
97+ const input = [ 4 , 5 , 6 ] ;
98+ const result = $Uint8ArrayLike . parse ( input ) ;
99+ expect ( result ) . toBeInstanceOf ( Uint8Array ) ;
100+ expect ( [ ...result ] ) . toEqual ( [ 4 , 5 , 6 ] ) ;
101+ } ) ;
102+
103+ it ( 'should fail to convert an invalid array' , ( ) => {
104+ expect ( $Uint8ArrayLike . safeParse ( [ - 1 , 2 , 3 ] ) . success ) . toBe ( false ) ;
105+ expect ( $Uint8ArrayLike . safeParse ( [ 1 , 2 , 256 ] ) . success ) . toBe ( false ) ;
106+ expect ( $Uint8ArrayLike . safeParse ( [ 1 , 2 , NaN ] ) . success ) . toBe ( false ) ;
107+ } ) ;
108+
109+ it ( 'should convert an ArrayBuffer to Uint8Array' , ( ) => {
110+ const buffer = new ArrayBuffer ( 3 ) ;
111+ const view = new Uint8Array ( buffer ) ;
112+ view . set ( [ 7 , 8 , 9 ] ) ;
113+ const result = $Uint8ArrayLike . safeParse ( buffer ) ;
114+ expect ( result . success ) . toBe ( true ) ;
115+ expect ( result . data ) . toBeInstanceOf ( Uint8Array ) ;
116+ expect ( [ ...result . data ! ] ) . toEqual ( [ 7 , 8 , 9 ] ) ;
117+ } ) ;
118+ } ) ;
0 commit comments