Skip to content

Commit

Permalink
fix: proper handling of simple json schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbourianes-kalisio committed May 17, 2024
1 parent 6d9b4c2 commit 5c50d0c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
12 changes: 11 additions & 1 deletion core/client/components/form/KForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,17 @@ async function build () {
// clone and configure the field
const cloneField = _.clone(field)
cloneField.name = property // assign a name to allow binding between properties and fields
cloneField.component = loadComponent(field.field.component)
let component = _.get(field.field, 'component', '')
if (!component) {
// Provide a default component based on schema value type when none is specified
if (field.type === 'number' || field.type === 'integer')
component = 'form/KNumberField'
else if (field.type === 'boolean')
component = 'form/KToggleField'
else if (field.type === 'string')
component = 'form/KTextField'
}
cloneField.component = loadComponent(component)
cloneField.reference = null // will be set once te field is rendered
cloneField.required = _.includes(schema.value.required, property) // add extra required info
// add the field to the list of fields to be rendered
Expand Down
5 changes: 4 additions & 1 deletion core/client/mixins/mixin.base-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export const baseField = {
},
computed: {
label () {
return this.$tie(_.get(this.properties.field, 'label', ''))
// Make use of 'description' metadata if nothing else is provided
// cf. https://ajv.js.org/json-schema.html#metadata-keywords
const description = _.get(this.properties, 'description', '')
return this.$tie(_.get(this.properties.field, 'label', description))
},
hasHelper () {
return !_.isEmpty(_.get(this.properties.field, 'helper', {}))
Expand Down
4 changes: 2 additions & 2 deletions map/client/components/KFeatureEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export default {
await this.$api.getService(this.service).patch(updatedFeature._id, _.pick(updatedFeature, ['properties']))
}
},
async loadLayerSchema () {
return updatePropertiesSchema(_.get(this.layer, 'schema.content'))
loadLayerSchema () {
return _.get(this.layer, 'schema.content')
},
async openModal () {
kCoreMixins.baseModal.methods.openModal.call(this)
Expand Down
28 changes: 0 additions & 28 deletions map/client/utils/utils.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,3 @@ export function generatePropertiesSchema (geoJson, name) {
})
return schema
}

export function updatePropertiesSchema (schema) {
const props = schema.properties
if (!props) return

const bestGuesses = {
undefined: 'form/KTextField',
object: 'form/KTextField',
string: 'form/KTextField',
number: 'form/KNumberField',
boolean: 'form/KToggleField'
}

// Loop over declared props and add best guesses to field components based on property type
for (const prop in props) {
const propEntry = props[prop]
// Field already here, skip entry
if (propEntry.field && propEntry.field.component) continue

propEntry.field = {
component: bestGuesses[propEntry.type],
label: prop,
helper: propEntry.description
}
}

return schema
}

0 comments on commit 5c50d0c

Please sign in to comment.