Skip to content

Commit 7b1a832

Browse files
authored
Merge pull request #544 from devtron-labs/fix/app-details-manifest
feat: add support for json schema 07 validation
2 parents 01fcfed + 537c293 commit 7b1a832

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.6.4",
3+
"version": "1.6.5",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/RJSF/Form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
import { forwardRef } from 'react'
1818
import RJSF from '@rjsf/core'
19-
import RJSFValidator from '@rjsf/validator-ajv8'
2019

2120
import { templates, widgets } from './config'
2221
import { FormProps } from './types'
2322
import { translateString } from './utils'
23+
import { SCHEMA_07_VALIDATOR_STRICT } from '@Shared/validations'
2424
import './rjsfForm.scss'
2525

2626
// Need to use this way because the default import was not working as expected
2727
// The default import resolves to an object intead of a function
2828
const Form = RJSF
29-
const validator = RJSFValidator
29+
const validator = SCHEMA_07_VALIDATOR_STRICT
3030

3131
export const RJSFForm = forwardRef((props: FormProps, ref: FormProps['ref']) => (
3232
<Form

src/Shared/validations.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { getSanitizedIframe } from '@Common/Helper'
18+
import { customizeValidator } from '@rjsf/validator-ajv8'
1819
import { PATTERNS } from '@Common/Constants'
1920
import { URLProtocolType } from './types'
2021
import { SKIP_LABEL_KEY_VALIDATION_PREFIX } from './constants'
@@ -378,10 +379,26 @@ export const validateSemanticVersioning = (version: string): ValidationResponseT
378379
export const validateDisplayName = (name: string): ValidationResponseType =>
379380
validateStringLength(name, DISPLAY_NAME_CONSTRAINTS.MAX_LIMIT, DISPLAY_NAME_CONSTRAINTS.MIN_LIMIT)
380381

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 => {
382396
try {
383397
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))
385402
}
386403
return {
387404
isValid: true,

0 commit comments

Comments
 (0)