1- import type { DataValidationCxt } from "../../lib/types"
1+ import type { DataValidationCxt , Context } from "../../lib/types"
22import Ajv from "../ajv"
33import chai from "../chai"
44
55chai . should ( )
66
7- type OasMode = "request" | "response"
8-
97function buildContext ( data : unknown ) : DataValidationCxt {
108 const dynamicAnchors : DataValidationCxt [ "dynamicAnchors" ] = { }
119 return {
@@ -21,17 +19,9 @@ function getAjv() {
2119 return new Ajv ( { passContext : true } )
2220}
2321
24- function getOasContext ( mode : OasMode , location ?: "requestBody" | "responseBody" ) {
25- return { oas : { mode, location} }
26- }
27-
2822type Validate = ReturnType < InstanceType < typeof Ajv > [ "compile" ] >
2923
30- function expectValid (
31- validate : Validate ,
32- data : unknown ,
33- ctx ?: { oas : { mode : OasMode ; location ?: "requestBody" | "responseBody" } }
34- ) {
24+ function expectValid ( validate : Validate , data : unknown , ctx ?: Context ) {
3525 const valid = ctx ? validate . call ( ctx , data , buildContext ( data ) ) : validate ( data )
3626 valid . should . equal ( true )
3727}
@@ -40,7 +30,7 @@ function expectInvalid(
4030 validate : Validate ,
4131 data : unknown ,
4232 keyword : "readOnly" | "writeOnly" | "required" ,
43- ctx ?: { oas : { mode : OasMode ; location ?: "requestBody" | "responseBody" } }
33+ ctx ?: Context
4434) {
4535 const valid = ctx ? validate . call ( ctx , data , buildContext ( data ) ) : validate ( data )
4636 valid . should . equal ( false )
@@ -78,7 +68,7 @@ describe("readOnly/writeOnly", () => {
7868
7969 it ( "should skip readOnly in request context and still require writeOnly" , ( ) => {
8070 const validate = getAjv ( ) . compile ( schemaWithRequired )
81- const requestCtx = getOasContext ( "request" , "requestBody" )
71+ const requestCtx : Context = { apiContext : "request" }
8272
8373 expectValid ( validate , { password : "secret" } , requestCtx )
8474 expectInvalid ( validate , { id : "1" , password : "secret" } , "readOnly" , requestCtx )
@@ -87,7 +77,7 @@ describe("readOnly/writeOnly", () => {
8777
8878 it ( "should skip writeOnly in response context and still require readOnly" , ( ) => {
8979 const validate = getAjv ( ) . compile ( schemaWithRequired )
90- const responseCtx = getOasContext ( "response" , "responseBody" )
80+ const responseCtx : Context = { apiContext : "response" }
9181
9282 expectValid ( validate , { id : "1" } , responseCtx )
9383 expectInvalid ( validate , { id : "1" , password : "secret" } , "writeOnly" , responseCtx )
@@ -98,14 +88,14 @@ describe("readOnly/writeOnly", () => {
9888 describe ( "presence validation with context" , ( ) => {
9989 it ( "should reject readOnly in request context" , ( ) => {
10090 const validate = getAjv ( ) . compile ( baseSchema )
101- const requestCtx = getOasContext ( "request" , "requestBody" )
91+ const requestCtx : Context = { apiContext : "request" }
10292
10393 expectInvalid ( validate , { id : "1" } , "readOnly" , requestCtx )
10494 } )
10595
10696 it ( "should reject writeOnly in response context" , ( ) => {
10797 const validate = getAjv ( ) . compile ( baseSchema )
108- const responseCtx = getOasContext ( "response" , "responseBody" )
98+ const responseCtx : Context = { apiContext : "response" }
10999
110100 expectInvalid ( validate , { password : "secret" } , "writeOnly" , responseCtx )
111101 } )
0 commit comments