Skip to content

Commit

Permalink
Merge pull request json-editor#1119 from germanbisurgi/feature/schema…
Browse files Browse the repository at this point in the history
…-error-messages

Feature/schema error messages
  • Loading branch information
schmunk42 authored Feb 25, 2022
2 parents 9753c07 + e16e313 commit 3f46462
Show file tree
Hide file tree
Showing 7 changed files with 891 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Unreleased

- Added feature: override error messages in schema options
- fixed arbitrary JSON pointers not returning the schema at the pointer path
- Add infoText for enums with format `checkbox` (default)
- updated `jodit` devDependency (jodit markup changes)
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,28 @@ By default, all instances of JSON Editor will use the `en` language. To overrid
JSONEditor.defaults.language = "es";
```

You can also override translations per editor in the it's schema options.

````json
"error_const": {
"type": "string",
"title": "error_const",
"const": "test",
"default": "something else",
"options": {
"error_messages": {
"en": {
"error_const": "CUSTOM EN: Value must be the constant value"
},
"es": {
"error_const": "CUSTOM ES: Value must be the constant value"
}
}
}
}
````


Button Customization
-----------------

Expand Down
11 changes: 9 additions & 2 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,18 @@ function upload (type, file, cbs) {
}

/* String translate function */
function translate (key, variables) {
function translate (key, variables, schema) {
let schemaMessages = {}

if (schema && schema.options && schema.options.error_messages && schema.options.error_messages[defaults.language]) {
schemaMessages = schema.options.error_messages[defaults.language]
}

const lang = defaults.languages[defaults.language]

if (!lang) throw new Error(`Unknown language ${defaults.language}`)

let string = lang[key] || defaults.languages[default_language][key] || key
let string = schemaMessages[key] || lang[key] || defaults.languages[default_language][key] || key

if (variables) {
for (let i = 0; i < variables.length; i++) {
Expand Down
70 changes: 36 additions & 34 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Validator {
return [{
path,
property: 'const',
message: this.translate('error_const')
message: this.translate('error_const', null, schema)
}]
}
return []
Expand All @@ -29,7 +29,7 @@ export class Validator {
return [{
path,
property: 'enum',
message: this.translate('error_enum')
message: this.translate('error_enum', null, schema)
}]
}
return []
Expand All @@ -54,7 +54,7 @@ export class Validator {
return [{
path,
property: 'anyOf',
message: this.translate('error_anyOf')
message: this.translate('error_anyOf', null, schema)
}]
}
return []
Expand All @@ -79,7 +79,7 @@ export class Validator {
errors.push({
path,
property: 'oneOf',
message: this.translate('error_oneOf', [valid])
message: this.translate('error_oneOf', [valid], schema)
})
errors.push(...oneofErrors)
}
Expand All @@ -90,7 +90,7 @@ export class Validator {
return [{
path,
property: 'not',
message: this.translate('error_not')
message: this.translate('error_not', null, schema)
}]
}
return []
Expand All @@ -103,7 +103,7 @@ export class Validator {
return [{
path,
property: 'type',
message: this.translate('error_type_union')
message: this.translate('error_type_union', null, schema)
}]
}
} else {
Expand All @@ -115,14 +115,14 @@ export class Validator {
return [{
path,
property: 'type',
message: this.translate('error_type', [schema.format])
message: this.translate('error_type', [schema.format], schema)
}]
}
} else if (!this._checkType(schema.type, value)) {
return [{
path,
property: 'type',
message: this.translate('error_type', [schema.type])
message: this.translate('error_type', [schema.type], schema)
}]
}
}
Expand All @@ -136,7 +136,7 @@ export class Validator {
return [{
path,
property: 'disallow',
message: this.translate('error_disallow_union')
message: this.translate('error_disallow_union', null, schema)
}]
}
} else {
Expand All @@ -145,7 +145,7 @@ export class Validator {
return [{
path,
property: 'disallow',
message: this.translate('error_disallow', [schema.disallow])
message: this.translate('error_disallow', [schema.disallow], schema)
}]
}
}
Expand Down Expand Up @@ -177,7 +177,8 @@ export class Validator {
property: 'maximum',
message: this.translate(
(schema.exclusiveMaximum ? 'error_maximum_excl' : 'error_maximum_incl'),
[schema.maximum]
[schema.maximum],
schema
)
}]
}
Expand All @@ -204,7 +205,8 @@ export class Validator {
property: 'minimum',
message: this.translate(
(schema.exclusiveMinimum ? 'error_minimum_excl' : 'error_minimum_incl'),
[schema.minimum]
[schema.minimum],
schema
)
}]
}
Expand All @@ -219,7 +221,7 @@ export class Validator {
errors.push({
path,
property: 'maxLength',
message: this.translate('error_maxLength', [schema.maxLength])
message: this.translate('error_maxLength', [schema.maxLength], schema)
})
}
return errors
Expand All @@ -230,7 +232,7 @@ export class Validator {
return [{
path,
property: 'minLength',
message: this.translate((schema.minLength === 1 ? 'error_notempty' : 'error_minLength'), [schema.minLength])
message: this.translate((schema.minLength === 1 ? 'error_notempty' : 'error_minLength'), [schema.minLength], schema)
}]
}
return []
Expand All @@ -241,7 +243,7 @@ export class Validator {
return [{
path,
property: 'pattern',
message: (schema.options && schema.options.patternmessage) ? schema.options.patternmessage : this.translate('error_pattern', [schema.pattern])
message: (schema.options && schema.options.patternmessage) ? schema.options.patternmessage : this.translate('error_pattern', [schema.pattern], schema)
}]
}
return []
Expand Down Expand Up @@ -269,7 +271,7 @@ export class Validator {
errors.push({
path,
property: 'additionalItems',
message: this.translate('error_additionalItems')
message: this.translate('error_additionalItems', null, schema)
})
break
/* Default for `additionalItems` is an empty schema */
Expand All @@ -291,7 +293,7 @@ export class Validator {
return [{
path,
property: 'maxItems',
message: this.translate('error_maxItems', [schema.maxItems])
message: this.translate('error_maxItems', [schema.maxItems], schema)
}]
}
return []
Expand All @@ -301,7 +303,7 @@ export class Validator {
return [{
path,
property: 'minItems',
message: this.translate('error_minItems', [schema.minItems])
message: this.translate('error_minItems', [schema.minItems], schema)
}]
}
return []
Expand All @@ -314,7 +316,7 @@ export class Validator {
return [{
path,
property: 'uniqueItems',
message: this.translate('error_uniqueItems')
message: this.translate('error_uniqueItems', null, schema)
}]
}
seen[valid] = true
Expand All @@ -329,7 +331,7 @@ export class Validator {
return [{
path,
property: 'maxProperties',
message: this.translate('error_maxProperties', [schema.maxProperties])
message: this.translate('error_maxProperties', [schema.maxProperties], schema)
}]
}
return []
Expand All @@ -339,7 +341,7 @@ export class Validator {
return [{
path,
property: 'minProperties',
message: this.translate('error_minProperties', [schema.minProperties])
message: this.translate('error_minProperties', [schema.minProperties], schema)
}]
}
return []
Expand All @@ -356,7 +358,7 @@ export class Validator {
errors.push({
path,
property: 'required',
message: this.translate('error_required', [schema && schema.properties && schema.properties[e] && schema.properties[e].title ? schema.properties[e].title : e])
message: this.translate('error_required', [schema && schema.properties && schema.properties[e] && schema.properties[e].title ? schema.properties[e].title : e], schema)
})
})
}
Expand Down Expand Up @@ -403,7 +405,7 @@ export class Validator {
errors.push({
path,
property: 'propertyNames',
message: this.translate('error_property_names_false', [k])
message: this.translate('error_property_names_false', [k], schema)
})
break
}
Expand Down Expand Up @@ -457,14 +459,14 @@ export class Validator {
errors.push({
path,
property: 'propertyNames',
message: this.translate('error_property_names_unsupported', [j])
message: this.translate('error_property_names_unsupported', [j], schema)
})
return false
}
errors.push({
path,
property: 'propertyNames',
message: this.translate(msg, [k])
message: this.translate(msg, [k], schema)
})
return false
})
Expand All @@ -483,7 +485,7 @@ export class Validator {
errors.push({
path,
property: 'additionalProperties',
message: this.translate('error_additional_properties', [k])
message: this.translate('error_additional_properties', [k], schema)
})
break
/* Allowed */
Expand All @@ -510,7 +512,7 @@ export class Validator {
errors.push({
path,
property: 'dependencies',
message: this.translate('error_dependency', [d])
message: this.translate('error_dependency', [d], schema)
})
}
})
Expand Down Expand Up @@ -638,7 +640,7 @@ export class Validator {
return [{
path,
property: 'required',
message: this.translate('error_notset')
message: this.translate('error_notset', null, schema)
}]
}
return []
Expand Down Expand Up @@ -713,7 +715,7 @@ export class Validator {
return [{
path,
property: 'format',
message: this.translate('error_pattern', ['^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'])
message: this.translate('error_pattern', ['^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'], schema)
}]
}
return []
Expand All @@ -736,7 +738,7 @@ export class Validator {
return [{
path,
property: schema.multipleOf ? 'multipleOf' : 'divisibleBy',
message: this.translate('error_multipleOf', [divisor])
message: this.translate('error_multipleOf', [divisor], schema)
}]
}
return []
Expand All @@ -750,14 +752,14 @@ export class Validator {
return [{
path,
property: 'format',
message: this.translate('error_invalid_epoch')
message: this.translate('error_invalid_epoch', null, schema)
}]
} else if (value !== Math.abs(parseInt(value))) {
/* not much to check for, so we assume value is ok if it's a positive number */
return [{
path,
property: 'format',
message: this.translate(`error_${schema.format.replace(/-/g, '_')}`, [dateFormat])
message: this.translate(`error_${schema.format.replace(/-/g, '_')}`, [dateFormat], schema)
}]
}
return []
Expand Down Expand Up @@ -786,7 +788,7 @@ export class Validator {
return [{
path,
property: 'format',
message: this.translate(`error_${editor.format.replace(/-/g, '_')}`, [errorDateFormat])
message: this.translate(`error_${editor.format.replace(/-/g, '_')}`, [errorDateFormat], schema)
}]
}
}
Expand Down Expand Up @@ -815,7 +817,7 @@ export class Validator {
return [{
path,
property: 'format',
message: this.translate(`error_${schema.format.replace(/-/g, '_')}`, [dateFormat])
message: this.translate(`error_${schema.format.replace(/-/g, '_')}`, [dateFormat], schema)
}]
}
} else if (editor) {
Expand Down
Loading

0 comments on commit 3f46462

Please sign in to comment.