DEV: Add experimental json_scheme site setting type (#12226)

This commit is contained in:
Penar Musaraj
2021-03-01 09:15:17 -05:00
committed by GitHub
parent bfa3e24e48
commit 6809cccd88
18 changed files with 411 additions and 3 deletions

View File

@ -17,9 +17,23 @@ class StringSettingValidator
return false
end
return valid_json?(val) if (@opts[:json_schema])
regex_match?(val)
end
def valid_json?(json)
# TODO: right now this is only ensuring JSON is valid
# but ideally we should validate against the schema
begin
JSON.parse(json)
rescue JSON::ParserError => e
@json_fail = true
return false
end
true
end
def error_message
if @regex_fail
I18n.t(@regex_error)
@ -31,6 +45,8 @@ class StringSettingValidator
else
I18n.t('site_settings.errors.invalid_string_max', max: @opts[:max])
end
elsif @json_fail
I18n.t('site_settings.errors.invalid_json')
else
I18n.t('site_settings.errors.invalid_string')
end