|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | import { getSanitizedIframe } from '@Common/Helper'
|
| 18 | +import { customizeValidator } from '@rjsf/validator-ajv8' |
18 | 19 | import { PATTERNS } from '@Common/Constants'
|
19 | 20 | import { URLProtocolType } from './types'
|
20 | 21 | import { SKIP_LABEL_KEY_VALIDATION_PREFIX } from './constants'
|
@@ -378,10 +379,26 @@ export const validateSemanticVersioning = (version: string): ValidationResponseT
|
378 | 379 | export const validateDisplayName = (name: string): ValidationResponseType =>
|
379 | 380 | validateStringLength(name, DISPLAY_NAME_CONSTRAINTS.MAX_LIMIT, DISPLAY_NAME_CONSTRAINTS.MIN_LIMIT)
|
380 | 381 |
|
381 |
| -export const validateJSON = (json: string): ValidationResponseType => { |
| 382 | +export const SCHEMA_07_VALIDATOR_STRICT = customizeValidator({ |
| 383 | + ajvOptionsOverrides: { |
| 384 | + strict: true, |
| 385 | + allowUnionTypes: true, |
| 386 | + }, |
| 387 | +}) |
| 388 | +SCHEMA_07_VALIDATOR_STRICT.ajv.addKeyword('hidden') |
| 389 | +SCHEMA_07_VALIDATOR_STRICT.ajv.addFormat('memory', /^\d+(\.\d+)?(Ki|Mi|Gi|Ti|Pi|Ei|KiB|MiB|GiB|TiB|PiB|EiB)?$/) |
| 390 | +SCHEMA_07_VALIDATOR_STRICT.ajv.addFormat('cpu', /^(?:\d+(\.\d+)?|(\d+)(m))$/) |
| 391 | + |
| 392 | +const SCHEMA_07_VALIDATOR = customizeValidator({ ajvOptionsOverrides: { strict: false } }) |
| 393 | +SCHEMA_07_VALIDATOR.ajv.addKeyword('hidden') |
| 394 | + |
| 395 | +export const doesJSONConformToSchema07 = (json: string, strict = false): ValidationResponseType => { |
382 | 396 | try {
|
383 | 397 | if (json) {
|
384 |
| - JSON.parse(json) |
| 398 | + // NOTE: if json is not parsable JSON.parse will through error |
| 399 | + // if validators can't be compiled from the parsed json schema then |
| 400 | + // provided json schema does not conform to json schema draft 07; again can throw error |
| 401 | + ;(strict ? SCHEMA_07_VALIDATOR_STRICT : SCHEMA_07_VALIDATOR).ajv.compile(JSON.parse(json)) |
385 | 402 | }
|
386 | 403 | return {
|
387 | 404 | isValid: true,
|
|
0 commit comments