|
9 | 9 | UiSchema, |
10 | 10 | ValidatorType, |
11 | 11 | } from '@rjsf/utils'; |
| 12 | +import localize from 'ajv-i18n'; |
12 | 13 |
|
13 | 14 | import AJV8Validator from '../src/validator'; |
14 | 15 | import { Localizer } from '../src'; |
@@ -2252,6 +2253,240 @@ describe('AJV8Validator', () => { |
2252 | 2253 | }); |
2253 | 2254 | }); |
2254 | 2255 | }); |
| 2256 | + describe('validating dependencies', () => { |
| 2257 | + beforeAll(() => { |
| 2258 | + validator = new AJV8Validator({ AjvClass: Ajv2019 }, localize.en as Localizer); |
| 2259 | + }); |
| 2260 | + it('should return an error when a dependent is missing', () => { |
| 2261 | + schema = { |
| 2262 | + type: 'object', |
| 2263 | + properties: { |
| 2264 | + creditCard: { |
| 2265 | + type: 'number', |
| 2266 | + }, |
| 2267 | + billingAddress: { |
| 2268 | + type: 'string', |
| 2269 | + }, |
| 2270 | + }, |
| 2271 | + dependentRequired: { |
| 2272 | + creditCard: ['billingAddress'], |
| 2273 | + }, |
| 2274 | + }; |
| 2275 | + const errors = validator.validateFormData({ creditCard: 1234567890 }, schema); |
| 2276 | + const errMessage = "must have property 'billingAddress' when property 'creditCard' is present"; |
| 2277 | + expect(errors.errors[0].message).toEqual(errMessage); |
| 2278 | + expect(errors.errors[0].stack).toEqual(errMessage); |
| 2279 | + expect(errors.errorSchema).toEqual({ |
| 2280 | + billingAddress: { |
| 2281 | + __errors: [errMessage], |
| 2282 | + }, |
| 2283 | + }); |
| 2284 | + expect(errors.errors[0].params.deps).toEqual('billingAddress'); |
| 2285 | + }); |
| 2286 | + it('should return an error when multiple dependents are missing', () => { |
| 2287 | + schema = { |
| 2288 | + type: 'object', |
| 2289 | + properties: { |
| 2290 | + creditCard: { |
| 2291 | + type: 'number', |
| 2292 | + }, |
| 2293 | + holderName: { |
| 2294 | + type: 'string', |
| 2295 | + }, |
| 2296 | + billingAddress: { |
| 2297 | + type: 'string', |
| 2298 | + }, |
| 2299 | + }, |
| 2300 | + dependentRequired: { |
| 2301 | + creditCard: ['holderName', 'billingAddress'], |
| 2302 | + }, |
| 2303 | + }; |
| 2304 | + const errors = validator.validateFormData({ creditCard: 1234567890 }, schema); |
| 2305 | + const errMessage = "must have properties 'holderName', 'billingAddress' when property 'creditCard' is present"; |
| 2306 | + expect(errors.errors[0].message).toEqual(errMessage); |
| 2307 | + expect(errors.errors[0].stack).toEqual(errMessage); |
| 2308 | + expect(errors.errorSchema).toEqual({ |
| 2309 | + billingAddress: { |
| 2310 | + __errors: [errMessage], |
| 2311 | + }, |
| 2312 | + holderName: { |
| 2313 | + __errors: [errMessage], |
| 2314 | + }, |
| 2315 | + }); |
| 2316 | + expect(errors.errors[0].params.deps).toEqual('holderName, billingAddress'); |
| 2317 | + }); |
| 2318 | + it('should return an error with title when a dependent is missing', () => { |
| 2319 | + schema = { |
| 2320 | + type: 'object', |
| 2321 | + properties: { |
| 2322 | + creditCard: { |
| 2323 | + type: 'number', |
| 2324 | + title: 'Credit card', |
| 2325 | + }, |
| 2326 | + billingAddress: { |
| 2327 | + type: 'string', |
| 2328 | + title: 'Billing address', |
| 2329 | + }, |
| 2330 | + }, |
| 2331 | + dependentRequired: { |
| 2332 | + creditCard: ['billingAddress'], |
| 2333 | + }, |
| 2334 | + }; |
| 2335 | + const errors = validator.validateFormData({ creditCard: 1234567890 }, schema); |
| 2336 | + const errMessage = "must have property 'Billing address' when property 'Credit card' is present"; |
| 2337 | + expect(errors.errors[0].message).toEqual(errMessage); |
| 2338 | + expect(errors.errors[0].stack).toEqual(errMessage); |
| 2339 | + expect(errors.errorSchema).toEqual({ |
| 2340 | + billingAddress: { |
| 2341 | + __errors: [errMessage], |
| 2342 | + }, |
| 2343 | + }); |
| 2344 | + expect(errors.errors[0].params.deps).toEqual('billingAddress'); |
| 2345 | + }); |
| 2346 | + it('should return an error with titles when multiple dependents are missing', () => { |
| 2347 | + schema = { |
| 2348 | + type: 'object', |
| 2349 | + properties: { |
| 2350 | + creditCard: { |
| 2351 | + type: 'number', |
| 2352 | + title: 'Credit card', |
| 2353 | + }, |
| 2354 | + holderName: { |
| 2355 | + type: 'string', |
| 2356 | + title: 'Holder name', |
| 2357 | + }, |
| 2358 | + billingAddress: { |
| 2359 | + type: 'string', |
| 2360 | + title: 'Billing address', |
| 2361 | + }, |
| 2362 | + }, |
| 2363 | + dependentRequired: { |
| 2364 | + creditCard: ['holderName', 'billingAddress'], |
| 2365 | + }, |
| 2366 | + }; |
| 2367 | + const errors = validator.validateFormData({ creditCard: 1234567890 }, schema); |
| 2368 | + const errMessage = |
| 2369 | + "must have properties 'Holder name', 'Billing address' when property 'Credit card' is present"; |
| 2370 | + expect(errors.errors[0].message).toEqual(errMessage); |
| 2371 | + expect(errors.errors[0].stack).toEqual(errMessage); |
| 2372 | + expect(errors.errorSchema).toEqual({ |
| 2373 | + billingAddress: { |
| 2374 | + __errors: [errMessage], |
| 2375 | + }, |
| 2376 | + holderName: { |
| 2377 | + __errors: [errMessage], |
| 2378 | + }, |
| 2379 | + }); |
| 2380 | + expect(errors.errors[0].params.deps).toEqual('holderName, billingAddress'); |
| 2381 | + }); |
| 2382 | + it('should return an error with uiSchema title when a dependent is missing', () => { |
| 2383 | + schema = { |
| 2384 | + type: 'object', |
| 2385 | + properties: { |
| 2386 | + creditCard: { |
| 2387 | + type: 'number', |
| 2388 | + }, |
| 2389 | + billingAddress: { |
| 2390 | + type: 'string', |
| 2391 | + }, |
| 2392 | + }, |
| 2393 | + dependentRequired: { |
| 2394 | + creditCard: ['billingAddress'], |
| 2395 | + }, |
| 2396 | + }; |
| 2397 | + const uiSchema: UiSchema = { |
| 2398 | + creditCard: { |
| 2399 | + 'ui:title': 'uiSchema Credit card', |
| 2400 | + }, |
| 2401 | + billingAddress: { |
| 2402 | + 'ui:title': 'uiSchema Billing address', |
| 2403 | + }, |
| 2404 | + }; |
| 2405 | + const errors = validator.validateFormData({ creditCard: 1234567890 }, schema, undefined, undefined, uiSchema); |
| 2406 | + const errMessage = |
| 2407 | + "must have property 'uiSchema Billing address' when property 'uiSchema Credit card' is present"; |
| 2408 | + expect(errors.errors[0].message).toEqual(errMessage); |
| 2409 | + expect(errors.errors[0].stack).toEqual(errMessage); |
| 2410 | + expect(errors.errorSchema).toEqual({ |
| 2411 | + billingAddress: { |
| 2412 | + __errors: [errMessage], |
| 2413 | + }, |
| 2414 | + }); |
| 2415 | + expect(errors.errors[0].params.deps).toEqual('billingAddress'); |
| 2416 | + }); |
| 2417 | + it('should return an error with uiSchema titles when multiple dependents are missing', () => { |
| 2418 | + schema = { |
| 2419 | + type: 'object', |
| 2420 | + properties: { |
| 2421 | + creditCard: { |
| 2422 | + type: 'number', |
| 2423 | + }, |
| 2424 | + holderName: { |
| 2425 | + type: 'string', |
| 2426 | + }, |
| 2427 | + billingAddress: { |
| 2428 | + type: 'string', |
| 2429 | + }, |
| 2430 | + }, |
| 2431 | + dependentRequired: { |
| 2432 | + creditCard: ['holderName', 'billingAddress'], |
| 2433 | + }, |
| 2434 | + }; |
| 2435 | + const uiSchema: UiSchema = { |
| 2436 | + creditCard: { |
| 2437 | + 'ui:title': 'uiSchema Credit card', |
| 2438 | + }, |
| 2439 | + holderName: { |
| 2440 | + 'ui:title': 'uiSchema Holder name', |
| 2441 | + }, |
| 2442 | + billingAddress: { |
| 2443 | + 'ui:title': 'uiSchema Billing address', |
| 2444 | + }, |
| 2445 | + }; |
| 2446 | + const errors = validator.validateFormData({ creditCard: 1234567890 }, schema, undefined, undefined, uiSchema); |
| 2447 | + const errMessage = |
| 2448 | + "must have properties 'uiSchema Holder name', 'uiSchema Billing address' when property 'uiSchema Credit card' is present"; |
| 2449 | + expect(errors.errors[0].message).toEqual(errMessage); |
| 2450 | + expect(errors.errors[0].stack).toEqual(errMessage); |
| 2451 | + expect(errors.errorSchema).toEqual({ |
| 2452 | + billingAddress: { |
| 2453 | + __errors: [errMessage], |
| 2454 | + }, |
| 2455 | + holderName: { |
| 2456 | + __errors: [errMessage], |
| 2457 | + }, |
| 2458 | + }); |
| 2459 | + expect(errors.errors[0].params.deps).toEqual('holderName, billingAddress'); |
| 2460 | + }); |
| 2461 | + it('should handle the case when errors are not present', () => { |
| 2462 | + schema = { |
| 2463 | + type: 'object', |
| 2464 | + properties: { |
| 2465 | + creditCard: { |
| 2466 | + type: 'number', |
| 2467 | + }, |
| 2468 | + holderName: { |
| 2469 | + type: 'string', |
| 2470 | + }, |
| 2471 | + billingAddress: { |
| 2472 | + type: 'string', |
| 2473 | + }, |
| 2474 | + }, |
| 2475 | + dependentRequired: { |
| 2476 | + creditCard: ['holderName', 'billingAddress'], |
| 2477 | + }, |
| 2478 | + }; |
| 2479 | + const errors = validator.validateFormData( |
| 2480 | + { |
| 2481 | + creditCard: 1234567890, |
| 2482 | + holderName: 'Alice', |
| 2483 | + billingAddress: 'El Camino Real', |
| 2484 | + }, |
| 2485 | + schema |
| 2486 | + ); |
| 2487 | + expect(errors.errors).toHaveLength(0); |
| 2488 | + }); |
| 2489 | + }); |
2255 | 2490 | }); |
2256 | 2491 | describe('validator.validateFormData(), custom options, localizer and Ajv2020', () => { |
2257 | 2492 | let validator: AJV8Validator; |
|
0 commit comments