@@ -178,6 +178,224 @@ Throws an error in the following cases:
178178
179179- ` RangeError ` : If [ maxErrors] ( #maxerrors ) is not positive integer.
180180
181+ ## assert
182+
183+ ``` ts
184+ import {
185+ assert ,
186+ number ,
187+ object ,
188+ string ,
189+ } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
190+ import {
191+ assertEquals ,
192+ assertIsError ,
193+ } from " https://deno.land/std/testing/asserts.ts" ;
194+
195+ const Profile = object ({ name: string , age: number });
196+
197+ try {
198+ assert (Profile , { name: null , age: null });
199+ } catch (e ) {
200+ assertIsError (e , AggregateError );
201+ assertEquals (e .errors .length , 2 );
202+ }
203+ ```
204+
205+ ### validation
206+
207+ Validation error configs.
208+
209+ #### error
210+
211+ Error constructor.
212+
213+ The default is ` ValidationError ` .
214+
215+ The example of specify validation error as:
216+
217+ ``` ts
218+ import { assert , between } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
219+
220+ assert (between (0 , 255 ), 256 , {
221+ failFast: true ,
222+ validation: { error: RangeError },
223+ });
224+ ```
225+
226+ #### message
227+
228+ Error message.
229+
230+ ``` ts
231+ import {
232+ assert ,
233+ type Validator ,
234+ } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
235+ import { assertEquals } from " https://deno.land/std/testing/asserts.ts" ;
236+
237+ declare const validator: Validator <unknown >;
238+ declare const input: unknown ;
239+ declare const message: string ;
240+
241+ try {
242+ assert (validator , input , { failFast: true , validation: { message } });
243+ } catch (e ) {
244+ assertEquals (e .message , message );
245+ }
246+ ```
247+
248+ #### cause
249+
250+ Original cause of the error.
251+
252+ ``` ts
253+ import {
254+ assert ,
255+ type Validator ,
256+ } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
257+ import { assertEquals } from " https://deno.land/std/testing/asserts.ts" ;
258+
259+ declare const validator: Validator <unknown >;
260+ declare const input: unknown ;
261+ declare const cause: ErrorConstructor ;
262+
263+ try {
264+ assert (validator , input , { failFast: true , validation: { cause } });
265+ } catch (e ) {
266+ assertEquals (e .cause , cause );
267+ }
268+ ```
269+
270+ ### Lazy vs Greedy
271+
272+ Validation by assert works with lazy or greedy.
273+
274+ Lazy terminates the evaluation as soon as it finds a validation error and
275+ reports only one validation error.
276+
277+ In contrast, greedy continues validation until the specified number of
278+ validation errors is reached or all validations are completed.
279+
280+ Also, validator has a lazy evaluation mechanism, so only as many validations are
281+ performed as needed.
282+
283+ By default, it operates as greedy.
284+
285+ ### failFast
286+
287+ If ` failFast ` is true, it works as lazy.
288+
289+ ``` ts
290+ import {
291+ assert ,
292+ ValidationError ,
293+ type Validator ,
294+ } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
295+ import {
296+ assertEquals ,
297+ assertIsError ,
298+ } from " https://deno.land/std/testing/asserts.ts" ;
299+
300+ declare const Profile: Validator <
301+ { name: unknown ; age: unknown },
302+ { name: string ; age: string }
303+ >;
304+
305+ try {
306+ assert (Profile , { name: null , age: null }, { failFast: true });
307+ } catch (e ) {
308+ assertIsError (e , ValidationError );
309+ }
310+ ```
311+
312+ The following fields can only be specified in greedy mode.
313+
314+ ### maxErrors option
315+
316+ The number of validation errors can be changed with ` maxErrors ` . For details,
317+ see [ maxErrors] ( #maxerrors )
318+
319+ ### aggregation
320+
321+ Aggregation error configs.
322+
323+ #### error
324+
325+ Specify custom ` AggregationErrorConstructor ` .
326+
327+ The default is ` AggregationError ` .
328+
329+ ``` ts
330+ import {
331+ assert ,
332+ type Validator ,
333+ } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
334+ import { assertIsError } from " https://deno.land/std/testing/asserts.ts" ;
335+
336+ declare const validator: Validator <unknown >;
337+ declare const input: unknown ;
338+ declare const error: AggregateErrorConstructor ;
339+
340+ try {
341+ assert (validator , input , { aggregation: { error } });
342+ } catch (e ) {
343+ assertIsError (e , error );
344+ }
345+ ```
346+
347+ #### message
348+
349+ Customize ` AggregationError ` message.
350+
351+ ``` ts
352+ import {
353+ assert ,
354+ type Validator ,
355+ } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
356+ import { assertEquals } from " https://deno.land/std/testing/asserts.ts" ;
357+
358+ declare const validator: Validator <unknown >;
359+ declare const input: unknown ;
360+ declare const message: string ;
361+
362+ try {
363+ assert (validator , input , { aggregation: { message } });
364+ } catch (e ) {
365+ assertEquals (e .message , message );
366+ }
367+ ```
368+
369+ #### cause
370+
371+ You can specify ` cause ` to express the cause of the ` AggregationError ` .
372+
373+ ``` ts
374+ import {
375+ assert ,
376+ type Validator ,
377+ } from " https://deno.land/x/abstruct@$VERSION/mod.ts" ;
378+ import { assertEquals } from " https://deno.land/std/testing/asserts.ts" ;
379+
380+ declare const validator: Validator <unknown >;
381+ declare const input: unknown ;
382+ declare const cause: Error ;
383+
384+ try {
385+ assert (validator , input , { aggregation: { cause } });
386+ } catch (e ) {
387+ assertEquals (e .cause , cause );
388+ }
389+ ```
390+
391+ ### Throwing error
392+
393+ Throws an error in the following cases:
394+
395+ - ` AggregateError ` : If assertion is fail.
396+ - ` ValidationError ` : If assertion is fail and [ failFast] ( #failfast ) is true.
397+ - Same as [ validate] ( #throwing-error ) .
398+
181399## License
182400
183401Copyright © 2023-present [ Tomoki Miyauci] ( https://github.com/TomokiMiyauci ) .
0 commit comments