| 273 | } |
| 274 | |
| 275 | function testSchema(schema) { |
| 276 | return { |
| 277 | simple: value => { |
| 278 | const causes = []; |
| 279 | Object.keys(schema).forEach(key => { |
| 280 | const nestedValidation = schema[key]; |
| 281 | try { |
| 282 | nestedValidation.check((value || {})[key]); |
| 283 | } catch (ex) { |
| 284 | ex.target = key; |
| 285 | causes.push(ex); |
| 286 | } |
| 287 | }); |
| 288 | if (causes.length > 0) { |
| 289 | throw causes; |
| 290 | } |
| 291 | return true; |
| 292 | }, |
| 293 | async: value => { |
| 294 | const causes = []; |
| 295 | const nested = Object.keys(schema).map(key => { |
| 296 | const nestedValidation = schema[key]; |
| 297 | return nestedValidation.testAsync((value || {})[key]).catch(ex => { |
| 298 | ex.target = key; |
| 299 | causes.push(ex); |
| 300 | }); |
| 301 | }); |
| 302 | return Promise.all(nested).then(() => { |
| 303 | if (causes.length > 0) { |
| 304 | throw causes; |
| 305 | } |
| 306 | |
| 307 | return true; |
| 308 | }); |
| 309 | }, |
| 310 | }; |
| 311 | } |
| 312 | |
| 313 | export default v8n; |