@@ -2,6 +2,7 @@ import { compileSchema } from "./compileSchema";
22import { strict as assert } from "assert" ;
33import { Draft , JsonError , SchemaNode } from "./types" ;
44import { draft2020 } from "./draft2020" ;
5+ import { ValidationPath } from "./Keyword" ;
56
67describe ( "compileSchema.validate" , ( ) => {
78 describe ( "integer" , ( ) => {
@@ -966,9 +967,9 @@ describe("compileSchema.validate - errorsAsync", () => {
966967 ...draft2020 . keywords ,
967968 {
968969 id : "async" ,
969- keyword : "async-error " ,
970+ keyword : "asyncError " ,
970971 addValidate : ( node ) => node . schema . asyncError != null ,
971- validate : async ( { node } ) : Promise < JsonError > => {
972+ validate : async ( { node } ) => {
972973 if ( node . schema . asyncError === false ) {
973974 return undefined ;
974975 }
@@ -981,26 +982,65 @@ describe("compileSchema.validate - errorsAsync", () => {
981982 }
982983 ]
983984 } ;
985+ } ) ;
984986
985- it ( "should resolve async validation returning no error" , async ( ) => {
986- const { errors, errorsAsync } = compileSchema (
987- { type : "number" , asyncError : false } ,
988- { drafts : [ draft ] }
989- ) . validate ( 4 ) ;
990- const asyncErrors = await Promise . all ( errorsAsync ) ;
991- assert . deepEqual ( errors . length , 0 ) ;
992- assert . deepEqual ( asyncErrors . length , 0 ) ;
993- } ) ;
987+ it ( "should resolve async validation returning no error" , async ( ) => {
988+ const { errors, errorsAsync } = compileSchema (
989+ { type : "number" , asyncError : false } ,
990+ { drafts : [ draft ] }
991+ ) . validate ( 4 ) ;
994992
995- it ( "should resolve async validation errors" , async ( ) => {
996- const { errorsAsync } = compileSchema (
997- { type : "number" , asyncError : true } ,
998- { drafts : [ draft ] }
999- ) . validate ( 4 ) ;
1000- const asyncErrors = await Promise . all ( errorsAsync ) ;
1001- assert . deepEqual ( asyncErrors . length , 1 ) ;
1002- assert . deepEqual ( asyncErrors [ 0 ] . code , "type-error" ) ;
1003- } ) ;
993+ const asyncErrors = ( await Promise . all ( errorsAsync ) ) . filter ( ( e ) => e != null ) ;
994+
995+ assert . deepEqual ( errors . length , 0 ) ;
996+ assert . deepEqual ( asyncErrors . length , 0 ) ;
997+ } ) ;
998+
999+ it ( "should resolve async validation errors" , async ( ) => {
1000+ const { errorsAsync } = compileSchema ( { type : "number" , asyncError : true } , { drafts : [ draft ] } ) . validate (
1001+ 4
1002+ ) ;
1003+
1004+ const asyncErrors = ( await Promise . all ( errorsAsync ) ) . filter ( ( e ) => e != null ) ;
1005+
1006+ assert ( asyncErrors . length === 1 ) ;
1007+
1008+ const error = asyncErrors [ 0 ] ;
1009+ assert ( error != null ) ;
1010+ assert . deepEqual ( error . code , "type-error" ) ;
1011+ } ) ;
1012+ } ) ;
1013+
1014+ describe ( "async validation - returned metadata" , ( ) => {
1015+ let draft : Draft ;
1016+ beforeEach ( ( ) => {
1017+ draft = {
1018+ ...draft2020 ,
1019+ keywords : [
1020+ ...draft2020 . keywords ,
1021+ {
1022+ id : "async" ,
1023+ keyword : "asyncError" ,
1024+ addValidate : ( node ) => node . schema . asyncError != null ,
1025+ validate : async ( { path } ) => {
1026+ return new Promise ( ( resolve ) => {
1027+ if ( path ) {
1028+ path [ path . length - 1 ] . meta = {
1029+ test : "yay"
1030+ } ;
1031+ }
1032+ resolve ( undefined ) ;
1033+ } ) ;
1034+ }
1035+ }
1036+ ]
1037+ } ;
1038+ } ) ;
1039+
1040+ it ( "should expose meta object on path" , async ( ) => {
1041+ const path : ValidationPath = [ ] ;
1042+ compileSchema ( { type : "number" , asyncError : true } , { drafts : [ draft ] } ) . validate ( 4 , "#" , path ) ;
1043+ assert . deepEqual ( path [ path . length - 1 ] . meta ?. test , "yay" ) ;
10041044 } ) ;
10051045 } ) ;
10061046} ) ;
0 commit comments