Skip to content

Commit

Permalink
fix: rename schemaRefs to schemaDefinitions (not breaking, just a ren…
Browse files Browse the repository at this point in the history
…amed function argument)
  • Loading branch information
josdejong committed Jan 26, 2022
1 parent 7cb9204 commit 0e7d653
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const editor = new JSONEditor({
```js
import { createAjvValidator } from 'svelte-jsoneditor'

const validator = createAjvValidator(schema, schemaRefs)
const validator = createAjvValidator(schema, schemaDefinitions)
```

- `onError(err: Error)`.
Expand All @@ -220,7 +220,7 @@ const editor = new JSONEditor({

function onRenderValue(props) {
// use the enum renderer, and fallback on the default renderer
return renderJSONSchemaEnum(props, schema, schemaRefs) || renderValue(props)
return renderJSONSchemaEnum(props, schema, schemaDefinitions) || renderValue(props)
}
```

Expand Down
4 changes: 2 additions & 2 deletions examples/browser/json_schema_validation.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h1>JSON schema validation</h1>
required: ['firstName', 'lastName']
}

const schemaRefs = {
const schemaDefinitions = {
job: {
title: 'Job description',
type: 'object',
Expand Down Expand Up @@ -127,7 +127,7 @@ <h1>JSON schema validation</h1>
props: {
content,
onChange: (update) => console.log('onChange', update),
validator: createAjvValidator(schema, schemaRefs)
validator: createAjvValidator(schema, schemaDefinitions)
}
})
</script>
Expand Down
14 changes: 8 additions & 6 deletions src/lib/plugins/validator/createAjvValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import { parseJSONPointerWithArrayIndices } from '../../utils/jsonPointer.js'
/**
* Create a JSON Schema validator powered by Ajv.
* @param {JSON} schema
* @param {JSON} [schemaRefs=undefined] An object containing JSON Schema references
* @return {function (json: JSON) : Array<Object>} Returns a valiation function
* @param {JSON} [schemaDefinitions=undefined]
* An object containing JSON Schema definitions
* which can be referenced using $ref
* @return {function (json: JSON) : Array<Object>} Returns a validation function
*/
export function createAjvValidator(schema, schemaRefs) {
export function createAjvValidator(schema, schemaDefinitions) {
const ajv = new (Ajv.default || Ajv)({
allErrors: true,
verbose: true,
$data: true
})

if (schemaRefs) {
Object.keys(schemaRefs).forEach((ref) => {
ajv.addSchema(schemaRefs[ref], ref)
if (schemaDefinitions) {
Object.keys(schemaDefinitions).forEach((ref) => {
ajv.addSchema(schemaDefinitions[ref], ref)
})
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/plugins/validator/createAjvValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const schema = {
required: ['firstName', 'lastName']
}

const schemaRefs = {
const schemaDefinitions = {
job: {
title: 'Job description',
type: 'object',
Expand Down Expand Up @@ -69,7 +69,7 @@ const schemaRefs = {

describe('createAjvValidator', () => {
it('should create a validate function', () => {
const validate = createAjvValidator(schema, schemaRefs)
const validate = createAjvValidator(schema, schemaDefinitions)

const invalidJson = {
firstName: 'John',
Expand Down
6 changes: 3 additions & 3 deletions src/lib/plugins/value/renderJSONSchemaEnum.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { getJSONSchemaOptions } from '../../utils/jsonSchemaUtils.js'
* have to fallback on the default valueRender function
* @param {RenderValueProps} props
* @param {JSON} schema
* @param {JSON} schemaRefs
* @param {JSON} schemaDefinitions
* @return {RenderValueConstructor[]}
*/
export function renderJSONSchemaEnum(props, schema, schemaRefs) {
const enumValues = getJSONSchemaOptions(schema, schemaRefs, props.path)
export function renderJSONSchemaEnum(props, schema, schemaDefinitions) {
const enumValues = getJSONSchemaOptions(schema, schemaDefinitions, props.path)

if (enumValues) {
const { value, path, readOnly, onPatch, onSelect, isSelected } = props
Expand Down
33 changes: 19 additions & 14 deletions src/lib/utils/jsonSchemaUtils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Find enum options for given path in a JSONSchema
* @param {JSON} schema
* @param {JSON} schemaRefs
* @param {JSON} [schemaDefinitions=undefined]
* @param {Path} path
* @returns {Array<any> | null}
*/
export function getJSONSchemaOptions(schema, schemaRefs, path) {
const schemaForPath = findSchema(schema, schemaRefs || {}, path)
export function getJSONSchemaOptions(schema, schemaDefinitions, path) {
const schemaForPath = findSchema(schema, schemaDefinitions || {}, path)

return schemaForPath ? findEnum(schemaForPath) : null
}
Expand Down Expand Up @@ -43,13 +43,18 @@ export function findEnum(schema) {
* Source: https://github.com/josdejong/jsoneditor/blob/develop/src/js/Node.js
*
* @param {JSON} topLevelSchema
* @param {JSON} schemaRefs
* @param {JSON} schemaDefinitions
* @param {Array.<string | number>} path
* @param {Object} currentSchema
* @return {Object | null}
* @private
*/
export function findSchema(topLevelSchema, schemaRefs, path, currentSchema = topLevelSchema) {
export function findSchema(
topLevelSchema,
schemaDefinitions,
path,
currentSchema = topLevelSchema
) {
const nextPath = path.slice(1, path.length)
const nextKey = path[0]

Expand All @@ -65,8 +70,8 @@ export function findSchema(topLevelSchema, schemaRefs, path, currentSchema = top

if ('$ref' in currentSchema && typeof currentSchema.$ref === 'string') {
const ref = currentSchema.$ref
if (ref in schemaRefs) {
currentSchema = schemaRefs[ref]
if (ref in schemaDefinitions) {
currentSchema = schemaDefinitions[ref]
} else if (ref.startsWith('#/')) {
const refPath = ref.substring(2).split('/')
currentSchema = topLevelSchema
Expand All @@ -79,15 +84,15 @@ export function findSchema(topLevelSchema, schemaRefs, path, currentSchema = top
}
} else if (ref.match(/#\//g)?.length === 1) {
const [schemaUrl, relativePath] = ref.split('#/')
if (schemaUrl in schemaRefs) {
const referencedSchema = schemaRefs[schemaUrl]
if (schemaUrl in schemaDefinitions) {
const referencedSchema = schemaDefinitions[schemaUrl]
const reference = { $ref: '#/'.concat(relativePath) }
const auxNextPath = []
auxNextPath.push(nextKey)
if (nextPath.length > 0) {
auxNextPath.push(...nextPath)
}
return findSchema(referencedSchema, schemaRefs, auxNextPath, reference)
return findSchema(referencedSchema, schemaDefinitions, auxNextPath, reference)
} else {
throw Error(`Unable to resolve reference ${ref}`)
}
Expand All @@ -109,7 +114,7 @@ export function findSchema(topLevelSchema, schemaRefs, path, currentSchema = top
nextKey in currentSchema.properties
) {
currentSchema = currentSchema.properties[nextKey]
return findSchema(topLevelSchema, schemaRefs, nextPath, currentSchema)
return findSchema(topLevelSchema, schemaDefinitions, nextPath, currentSchema)
}
if (
typeof currentSchema.patternProperties === 'object' &&
Expand All @@ -118,13 +123,13 @@ export function findSchema(topLevelSchema, schemaRefs, path, currentSchema = top
for (const prop in currentSchema.patternProperties) {
if (nextKey.match(prop)) {
currentSchema = currentSchema.patternProperties[prop]
return findSchema(topLevelSchema, schemaRefs, nextPath, currentSchema)
return findSchema(topLevelSchema, schemaDefinitions, nextPath, currentSchema)
}
}
}
if (typeof currentSchema.additionalProperties === 'object') {
currentSchema = currentSchema.additionalProperties
return findSchema(topLevelSchema, schemaRefs, nextPath, currentSchema)
return findSchema(topLevelSchema, schemaDefinitions, nextPath, currentSchema)
}
continue
}
Expand All @@ -134,7 +139,7 @@ export function findSchema(topLevelSchema, schemaRefs, path, currentSchema = top
currentSchema.items !== null
) {
currentSchema = currentSchema.items
return findSchema(topLevelSchema, schemaRefs, nextPath, currentSchema)
return findSchema(topLevelSchema, schemaDefinitions, nextPath, currentSchema)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils/jsonSchemaUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('jsonSchemaUtils', () => {
}
}
}
const schemaRefs = {
const schemaDefinitions = {
second_schema: {
definitions: {
some_def: {
Expand All @@ -130,7 +130,7 @@ describe('jsonSchemaUtils', () => {
const expectedSchema = {
enum: [1, 2, 3]
}
assert.deepStrictEqual(findSchema(schema, schemaRefs, path), expectedSchema)
assert.deepStrictEqual(findSchema(schema, schemaDefinitions, path), expectedSchema)
})

it('should find array referenced schema within multi-level object properties', () => {
Expand All @@ -145,7 +145,7 @@ describe('jsonSchemaUtils', () => {
}
}
}
const schemaRefs = {
const schemaDefinitions = {
second_schema: {
definitions: {
some_def: {
Expand All @@ -163,7 +163,7 @@ describe('jsonSchemaUtils', () => {
const expectedSchema = {
enum: [1, 2, 3]
}
assert.deepStrictEqual(findSchema(schema, schemaRefs, path), expectedSchema)
assert.deepStrictEqual(findSchema(schema, schemaDefinitions, path), expectedSchema)
})

it('should return null for path that has no schema', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/examples/json_schema_validation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
required: ['firstName', 'lastName']
}
const schemaRefs = {
const schemaDefinitions = {
job: {
title: 'Job description',
type: 'object',
Expand Down Expand Up @@ -73,11 +73,11 @@
}
// create a JSON schema validator powered by Ajv
const validator = createAjvValidator(schema, schemaRefs)
const validator = createAjvValidator(schema, schemaDefinitions)
// enable rendering a select box for enums
function onRenderValue(props) {
return renderJSONSchemaEnum(props, schema, schemaRefs) || renderValue(props)
return renderJSONSchemaEnum(props, schema, schemaDefinitions) || renderValue(props)
}
let content = {
Expand Down

0 comments on commit 0e7d653

Please sign in to comment.