Skip to content

Commit 87ba5bd

Browse files
committed
update tests
1 parent 824445a commit 87ba5bd

2 files changed

Lines changed: 418 additions & 0 deletions

File tree

src/tests/__snapshots__/compileValueSchema.test.ts.snap

Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,3 +2111,346 @@ function obj2(path, value, context) {
21112111
return value;
21122112
}"
21132113
`;
2114+
2115+
exports[`OpenAPI 3.1 $ref with sibling keywords $ref with additionalProperties merges constraints into additionalProperties 1`] = `
2116+
"/**
2117+
Validate a request against the OpenAPI spec
2118+
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
2119+
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | string | null } }} [context] - Context object to pass to validation functions
2120+
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
2121+
*/
2122+
export function validateRequest(request, context) {
2123+
return new RequestError(404, 'no operation match path');
2124+
}
2125+
/**
2126+
Map of all components defined in the spec to their validation functions.
2127+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
2128+
*/
2129+
export const componentSchemas = { 'StringMap': obj2 };
2130+
export class RequestError extends Error {
2131+
/** @param {number} code HTTP code for the error
2132+
@param {string} message The error message*/
2133+
constructor(code, message) {
2134+
super(message);
2135+
/** @type {number} HTTP code for the error*/
2136+
this.code = code;
2137+
}
2138+
}
2139+
export class ValidationError extends RequestError {
2140+
/** @param {string[]} path The path that failed validation
2141+
@param {string} message The error message*/
2142+
constructor(path, message) {
2143+
super(409, message);
2144+
/** @type {string[]} The path that failed validation*/
2145+
this.path = path;
2146+
}
2147+
}
2148+
function obj1(path, value, context) {
2149+
if (typeof value !== 'string') {
2150+
return new ValidationError(path, 'expected a string');
2151+
}
2152+
if (value.length > 64) {
2153+
return new ValidationError(path, 'expected at most 64 characters');
2154+
}
2155+
return value;
2156+
}
2157+
function obj0(path, value, context) {
2158+
if (typeof value !== 'object' || value === null) {
2159+
return new ValidationError(path, 'expected an object');
2160+
}
2161+
const keys = new Set(Object.keys(value));
2162+
for (const key of keys) {
2163+
const result = obj1([
2164+
...path,
2165+
key
2166+
], value[key], context);
2167+
if (result instanceof ValidationError) {
2168+
return result;
2169+
}
2170+
value[key] = result;
2171+
}
2172+
return value;
2173+
}
2174+
function obj3(path, value, context) {
2175+
if (typeof value !== 'string') {
2176+
return new ValidationError(path, 'expected a string');
2177+
}
2178+
return value;
2179+
}
2180+
function obj2(path, value, context) {
2181+
if (typeof value !== 'object' || value === null) {
2182+
return new ValidationError(path, 'expected an object');
2183+
}
2184+
const keys = new Set(Object.keys(value));
2185+
for (const key of keys) {
2186+
const result = obj3([
2187+
...path,
2188+
key
2189+
], value[key], context);
2190+
if (result instanceof ValidationError) {
2191+
return result;
2192+
}
2193+
value[key] = result;
2194+
}
2195+
return value;
2196+
}"
2197+
`;
2198+
2199+
exports[`OpenAPI 3.1 $ref with sibling keywords $ref with additionalProperties merges multiple constraints 1`] = `
2200+
"/**
2201+
Validate a request against the OpenAPI spec
2202+
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
2203+
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | string | null } }} [context] - Context object to pass to validation functions
2204+
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
2205+
*/
2206+
export function validateRequest(request, context) {
2207+
return new RequestError(404, 'no operation match path');
2208+
}
2209+
/**
2210+
Map of all components defined in the spec to their validation functions.
2211+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
2212+
*/
2213+
export const componentSchemas = { 'StringMap': obj3 };
2214+
export class RequestError extends Error {
2215+
/** @param {number} code HTTP code for the error
2216+
@param {string} message The error message*/
2217+
constructor(code, message) {
2218+
super(message);
2219+
/** @type {number} HTTP code for the error*/
2220+
this.code = code;
2221+
}
2222+
}
2223+
export class ValidationError extends RequestError {
2224+
/** @param {string[]} path The path that failed validation
2225+
@param {string} message The error message*/
2226+
constructor(path, message) {
2227+
super(409, message);
2228+
/** @type {string[]} The path that failed validation*/
2229+
this.path = path;
2230+
}
2231+
}
2232+
const obj2 = new RegExp('^[a-z]+$');
2233+
function obj1(path, value, context) {
2234+
if (typeof value !== 'string') {
2235+
return new ValidationError(path, 'expected a string');
2236+
}
2237+
if (value.length < 1) {
2238+
return new ValidationError(path, 'expected at least 1 characters');
2239+
}
2240+
if (!obj2.test(value)) {
2241+
return new ValidationError(path, 'expected to match the pattern "^[a-z]+$"');
2242+
}
2243+
return value;
2244+
}
2245+
function obj0(path, value, context) {
2246+
if (typeof value !== 'object' || value === null) {
2247+
return new ValidationError(path, 'expected an object');
2248+
}
2249+
const keys = new Set(Object.keys(value));
2250+
for (const key of keys) {
2251+
const result = obj1([
2252+
...path,
2253+
key
2254+
], value[key], context);
2255+
if (result instanceof ValidationError) {
2256+
return result;
2257+
}
2258+
value[key] = result;
2259+
}
2260+
return value;
2261+
}
2262+
function obj4(path, value, context) {
2263+
if (typeof value !== 'string') {
2264+
return new ValidationError(path, 'expected a string');
2265+
}
2266+
return value;
2267+
}
2268+
function obj3(path, value, context) {
2269+
if (typeof value !== 'object' || value === null) {
2270+
return new ValidationError(path, 'expected an object');
2271+
}
2272+
const keys = new Set(Object.keys(value));
2273+
for (const key of keys) {
2274+
const result = obj4([
2275+
...path,
2276+
key
2277+
], value[key], context);
2278+
if (result instanceof ValidationError) {
2279+
return result;
2280+
}
2281+
value[key] = result;
2282+
}
2283+
return value;
2284+
}"
2285+
`;
2286+
2287+
exports[`OpenAPI 3.1 $ref with sibling keywords $ref with items merges constraints into items 1`] = `
2288+
"/**
2289+
Validate a request against the OpenAPI spec
2290+
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
2291+
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | string | null } }} [context] - Context object to pass to validation functions
2292+
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
2293+
*/
2294+
export function validateRequest(request, context) {
2295+
return new RequestError(404, 'no operation match path');
2296+
}
2297+
/**
2298+
Map of all components defined in the spec to their validation functions.
2299+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
2300+
*/
2301+
export const componentSchemas = { 'StringArray': obj2 };
2302+
export class RequestError extends Error {
2303+
/** @param {number} code HTTP code for the error
2304+
@param {string} message The error message*/
2305+
constructor(code, message) {
2306+
super(message);
2307+
/** @type {number} HTTP code for the error*/
2308+
this.code = code;
2309+
}
2310+
}
2311+
export class ValidationError extends RequestError {
2312+
/** @param {string[]} path The path that failed validation
2313+
@param {string} message The error message*/
2314+
constructor(path, message) {
2315+
super(409, message);
2316+
/** @type {string[]} The path that failed validation*/
2317+
this.path = path;
2318+
}
2319+
}
2320+
function obj1(path, value, context) {
2321+
if (typeof value !== 'string') {
2322+
return new ValidationError(path, 'expected a string');
2323+
}
2324+
if (value.length < 1) {
2325+
return new ValidationError(path, 'expected at least 1 characters');
2326+
}
2327+
return value;
2328+
}
2329+
function obj0(path, value, context) {
2330+
if (!Array.isArray(value)) {
2331+
return new ValidationError(path, 'expected an array');
2332+
}
2333+
for (let i = 0; i < value.length; i++) {
2334+
const itemResult = obj1([
2335+
...path,
2336+
i
2337+
], value[i], context);
2338+
if (itemResult instanceof ValidationError) {
2339+
return itemResult;
2340+
}
2341+
value[i] = itemResult;
2342+
}
2343+
return value;
2344+
}
2345+
function obj3(path, value, context) {
2346+
if (typeof value !== 'string') {
2347+
return new ValidationError(path, 'expected a string');
2348+
}
2349+
return value;
2350+
}
2351+
function obj2(path, value, context) {
2352+
if (!Array.isArray(value)) {
2353+
return new ValidationError(path, 'expected an array');
2354+
}
2355+
for (let i = 0; i < value.length; i++) {
2356+
const itemResult = obj3([
2357+
...path,
2358+
i
2359+
], value[i], context);
2360+
if (itemResult instanceof ValidationError) {
2361+
return itemResult;
2362+
}
2363+
value[i] = itemResult;
2364+
}
2365+
return value;
2366+
}"
2367+
`;
2368+
2369+
exports[`OpenAPI 3.1 $ref with sibling keywords $ref with items merges multiple constraints 1`] = `
2370+
"/**
2371+
Validate a request against the OpenAPI spec
2372+
@param {{ method: string; path: string; body?: any; query: Record<string, string | string[]>; headers: Record<string, string>; }} request - Input request to validate
2373+
@param {{ stringFormats?: { [format: string]: (value: string, path: string[]) => ValidationError | string | null } }} [context] - Context object to pass to validation functions
2374+
@returns {{ operationId?: string; params: Record<string, string>; query: Record<string, string | string[]>; body?: any; headers: Record<string, string>; }}
2375+
*/
2376+
export function validateRequest(request, context) {
2377+
return new RequestError(404, 'no operation match path');
2378+
}
2379+
/**
2380+
Map of all components defined in the spec to their validation functions.
2381+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
2382+
*/
2383+
export const componentSchemas = { 'StringArray': obj3 };
2384+
export class RequestError extends Error {
2385+
/** @param {number} code HTTP code for the error
2386+
@param {string} message The error message*/
2387+
constructor(code, message) {
2388+
super(message);
2389+
/** @type {number} HTTP code for the error*/
2390+
this.code = code;
2391+
}
2392+
}
2393+
export class ValidationError extends RequestError {
2394+
/** @param {string[]} path The path that failed validation
2395+
@param {string} message The error message*/
2396+
constructor(path, message) {
2397+
super(409, message);
2398+
/** @type {string[]} The path that failed validation*/
2399+
this.path = path;
2400+
}
2401+
}
2402+
const obj2 = new RegExp('^[a-z]+$');
2403+
function obj1(path, value, context) {
2404+
if (typeof value !== 'string') {
2405+
return new ValidationError(path, 'expected a string');
2406+
}
2407+
if (value.length < 1) {
2408+
return new ValidationError(path, 'expected at least 1 characters');
2409+
}
2410+
if (value.length > 100) {
2411+
return new ValidationError(path, 'expected at most 100 characters');
2412+
}
2413+
if (!obj2.test(value)) {
2414+
return new ValidationError(path, 'expected to match the pattern "^[a-z]+$"');
2415+
}
2416+
return value;
2417+
}
2418+
function obj0(path, value, context) {
2419+
if (!Array.isArray(value)) {
2420+
return new ValidationError(path, 'expected an array');
2421+
}
2422+
for (let i = 0; i < value.length; i++) {
2423+
const itemResult = obj1([
2424+
...path,
2425+
i
2426+
], value[i], context);
2427+
if (itemResult instanceof ValidationError) {
2428+
return itemResult;
2429+
}
2430+
value[i] = itemResult;
2431+
}
2432+
return value;
2433+
}
2434+
function obj4(path, value, context) {
2435+
if (typeof value !== 'string') {
2436+
return new ValidationError(path, 'expected a string');
2437+
}
2438+
return value;
2439+
}
2440+
function obj3(path, value, context) {
2441+
if (!Array.isArray(value)) {
2442+
return new ValidationError(path, 'expected an array');
2443+
}
2444+
for (let i = 0; i < value.length; i++) {
2445+
const itemResult = obj4([
2446+
...path,
2447+
i
2448+
], value[i], context);
2449+
if (itemResult instanceof ValidationError) {
2450+
return itemResult;
2451+
}
2452+
value[i] = itemResult;
2453+
}
2454+
return value;
2455+
}"
2456+
`;

0 commit comments

Comments
 (0)