1+ import { b } from '@jsonjoy.com/util/lib/buffers/b' ;
12import { ValidationError } from '../../../constants' ;
23import { type OrSchema , s , type Schema } from '../../../schema' ;
34import { TypeSystem } from '../../../system' ;
@@ -125,7 +126,7 @@ describe('"str" type', () => {
125126 exec ( type , null , { code : 'STR' , errno : ValidationError . STR , message : 'Not a string.' , path : [ ] } ) ;
126127 } ) ;
127128
128- test ( 'validates minLength ' , ( ) => {
129+ test ( 'validates "min" ' , ( ) => {
129130 const type = s . String ( { min : 3 } ) ;
130131 exec ( type , 'asdf' , null ) ;
131132 exec ( type , '' , {
@@ -142,7 +143,7 @@ describe('"str" type', () => {
142143 } ) ;
143144 } ) ;
144145
145- test ( 'validates maxLength ' , ( ) => {
146+ test ( 'validates "max" ' , ( ) => {
146147 const type = s . String ( { max : 5 } ) ;
147148 exec ( type , '' , null ) ;
148149 exec ( type , 'asdf' , null ) ;
@@ -161,7 +162,7 @@ describe('"str" type', () => {
161162 } ) ;
162163 } ) ;
163164
164- test ( 'validates minLength and maxLength ' , ( ) => {
165+ test ( 'validates "min" and "max" ' , ( ) => {
165166 const type = s . String ( { min : 3 , max : 5 } ) ;
166167 exec ( type , 'aaa' , null ) ;
167168 exec ( type , 'bbbb' , null ) ;
@@ -186,6 +187,112 @@ describe('"str" type', () => {
186187 } ) ;
187188 } ) ;
188189
190+ test ( 'validates "min" and "max" of equal size' , ( ) => {
191+ const type = s . String ( { min : 4 , max : 4 } ) ;
192+ exec ( type , 'aaa' , {
193+ code : 'STR_LEN' ,
194+ errno : ValidationError . STR_LEN ,
195+ message : 'Invalid string length.' ,
196+ path : [ ] ,
197+ } ) ;
198+ exec ( type , 'bbbb' , null ) ;
199+ exec ( type , 'vvvvv' , {
200+ code : 'STR_LEN' ,
201+ errno : ValidationError . STR_LEN ,
202+ message : 'Invalid string length.' ,
203+ path : [ ] ,
204+ } ) ;
205+ exec ( type , '' , {
206+ code : 'STR_LEN' ,
207+ errno : ValidationError . STR_LEN ,
208+ message : 'Invalid string length.' ,
209+ path : [ ] ,
210+ } ) ;
211+ exec ( type , 'asdfdf' , {
212+ code : 'STR_LEN' ,
213+ errno : ValidationError . STR_LEN ,
214+ message : 'Invalid string length.' ,
215+ path : [ ] ,
216+ } ) ;
217+ exec ( type , 'aasdf sdfdf' , {
218+ code : 'STR_LEN' ,
219+ errno : ValidationError . STR_LEN ,
220+ message : 'Invalid string length.' ,
221+ path : [ ] ,
222+ } ) ;
223+ } ) ;
224+ } ) ;
225+
226+ describe ( '"bin" type' , ( ) => {
227+ test ( 'validates a binary blob' , ( ) => {
228+ const type = s . bin ;
229+ exec ( type , b ( ) , null ) ;
230+ exec ( type , b ( 1 , 2 , 3 ) , null ) ;
231+ exec ( type , 123 , { code : 'BIN' , errno : ValidationError . BIN , message : 'Not a binary.' , path : [ ] } ) ;
232+ exec ( type , null , { code : 'BIN' , errno : ValidationError . BIN , message : 'Not a binary.' , path : [ ] } ) ;
233+ } ) ;
234+
235+ test ( 'validates "min"' , ( ) => {
236+ const type = s . Binary ( s . any , { min : 3 } ) ;
237+ exec ( type , b ( 1 , 2 , 3 , 4 ) , null ) ;
238+ exec ( type , b ( ) , {
239+ code : 'BIN_LEN' ,
240+ errno : ValidationError . BIN_LEN ,
241+ message : 'Invalid binary length.' ,
242+ path : [ ] ,
243+ } ) ;
244+ exec ( type , b ( 1 , 2 ) , {
245+ code : 'BIN_LEN' ,
246+ errno : ValidationError . BIN_LEN ,
247+ message : 'Invalid binary length.' ,
248+ path : [ ] ,
249+ } ) ;
250+ } ) ;
251+
252+ test ( 'validates "max"' , ( ) => {
253+ const type = s . Binary ( s . any , { max : 5 } ) ;
254+ exec ( type , b ( ) , null ) ;
255+ exec ( type , b ( 1 , 2 , 3 , 4 ) , null ) ;
256+ exec ( type , b ( 1 , 2 , 3 , 4 , 5 ) , null ) ;
257+ exec ( type , b ( 1 , 2 , 3 , 4 , 5 , 6 ) , {
258+ code : 'BIN_LEN' ,
259+ errno : ValidationError . BIN_LEN ,
260+ message : 'Invalid binary length.' ,
261+ path : [ ] ,
262+ } ) ;
263+ exec ( type , b ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) , {
264+ code : 'BIN_LEN' ,
265+ errno : ValidationError . BIN_LEN ,
266+ message : 'Invalid binary length.' ,
267+ path : [ ] ,
268+ } ) ;
269+ } ) ;
270+
271+ test ( 'validates "min" and "max"' , ( ) => {
272+ const type = s . Binary ( s . any , { min : 3 , max : 5 } ) ;
273+ exec ( type , b ( 1 , 2 , 3 ) , null ) ;
274+ exec ( type , b ( 1 , 2 , 3 , 4 ) , null ) ;
275+ exec ( type , b ( 1 , 2 , 3 , 4 , 5 ) , null ) ;
276+ exec ( type , b ( ) , {
277+ code : 'BIN_LEN' ,
278+ errno : ValidationError . BIN_LEN ,
279+ message : 'Invalid binary length.' ,
280+ path : [ ] ,
281+ } ) ;
282+ exec ( type , b ( 1 , 2 , 3 , 4 , 5 , 6 ) , {
283+ code : 'BIN_LEN' ,
284+ errno : ValidationError . BIN_LEN ,
285+ message : 'Invalid binary length.' ,
286+ path : [ ] ,
287+ } ) ;
288+ exec ( type , b ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) , {
289+ code : 'BIN_LEN' ,
290+ errno : ValidationError . BIN_LEN ,
291+ message : 'Invalid binary length.' ,
292+ path : [ ] ,
293+ } ) ;
294+ } ) ;
295+
189296 test ( 'validates minLength and maxLength of equal size' , ( ) => {
190297 const type = s . String ( { min : 4 , max : 4 } ) ;
191298 exec ( type , 'aaa' , {
0 commit comments