Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/modify-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,35 @@ interface ModifyResult {
}

/**
* Converts a short path to a full path
* JSON Schema keywords that are direct keys on a schema node (not inside `properties`)
*/
const SCHEMA_PASSTHROUGH_KEYS = new Set([
'items',
])

/**
* Converts a short path to a full path, inserting `.properties.` before each
* segment that is a regular field name. JSON Schema structural keywords (e.g.
* `items`, `allOf`) are kept as-is because they are direct keys on a schema
* node, not nested inside `properties`.
*
* @param {string} path
* @returns {string} The full path
* @example
* shortToFullPath('foo.bar') // 'foo.properties.bar'
* shortToFullPath('foo.bar') // 'foo.properties.bar'
* shortToFullPath('foo.items.bar') // 'foo.items.properties.bar'
*/
function shortToFullPath(path: string) {
return path.replaceAll('.', '.properties.')
const segments = path.split('.')
return segments.reduce((acc, segment, index) => {
if (index === 0) {
return segment
}
if (SCHEMA_PASSTHROUGH_KEYS.has(segment)) {
return `${acc}.${segment}`
}
return `${acc}.properties.${segment}`
}, '')
}

/**
Expand Down Expand Up @@ -203,6 +224,13 @@ function rewriteAllFields(schema: JsfSchema, configCallback: ModifyConfig['allFi
parent: fieldName,
})
}

// Array item fields, go recursive
if (fieldAttrs.items) {
rewriteAllFields(fieldAttrs.items, configCallback, {
parent: fieldName,
})
}
})
}

Expand Down
85 changes: 84 additions & 1 deletion test/modify-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ describe('modifySchema', () => {
},
},
},
pet_clinics: {
'title': 'Pet clinics',
'type': 'array',
'x-jsf-presentation': {
inputType: 'group-array',
},
'items': {
type: 'object',
properties: {
name: {
'title': 'name',
'type': 'string',
'x-jsf-presentation': {
inputType: 'select',
},
},
address: {
'title': 'address',
'type': 'string',
'x-jsf-presentation': {
inputType: 'text',
},
},
},
},
},
},
'required': ['has_pet'],
'x-jsf-order': ['has_pet', 'pet_name', 'pet_age', 'pet_fat', 'pet_address'],
Expand Down Expand Up @@ -633,6 +659,63 @@ describe('modifySchema', () => {
},
})
})

it('replace field attrs inside array items', () => {
const result = modifySchema(schemaPet, {
fields: {
'pet_clinics.items.name': {
title: 'Clinic name',
},
},
})

expect(result.schema).toMatchObject({
properties: {
pet_clinics: {
items: {
properties: {
name: {
title: 'Clinic name',
},
},
},
},
},
})
})

it('replace field attrs inside array items using allFields', () => {
Comment thread
nitin-remote marked this conversation as resolved.
const result = modifySchema(schemaPet, {
allFields: (fieldName, fieldAttrs) => {
if (fieldAttrs['x-jsf-presentation']?.inputType === 'select') {
return {
'title': 'Clinic name',
'x-jsf-presentation': {
inputType: 'text',
},
}
}
return {}
},
})

expect(result.schema).toMatchObject({
properties: {
pet_clinics: {
items: {
properties: {
name: {
'title': 'Clinic name',
'x-jsf-presentation': {
inputType: 'text',
},
},
},
},
},
},
})
})
})

describe('supporting custom attributes', () => {
Expand Down Expand Up @@ -700,7 +783,7 @@ describe('modifySchema', () => {
},
})

// Assert all the other propreties are kept
// Assert all the other properties are kept
expect(result.schema).toMatchObject(invoiceSchema)

expect(result.schema).toMatchObject({
Expand Down
Loading