Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active July 18, 2018 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/babfd89c2d259308eec79577971ca2ea to your computer and use it in GitHub Desktop.
Save typeoneerror/babfd89c2d259308eec79577971ca2ea to your computer and use it in GitHub Desktop.
Key-Value CSS validation concept

Iheanyi,

I realize your app may not be ruby-based, but here's how I've worked with this dilemma in mind in our app. To start, yes, we don't necessarily support custom CSS, but I do have a database field I (as admin) can add custom CSS to a customer's account template (in the case they have "premium support" or something). Our publicly customizable style settings are key:value based and stored as a JSON object in psql as you suggested might be an option. Here's how I make it less unweildy:

Field in the database:

t.json :settings

Then we have a validator that validates against a schema (see https://github.com/mirego/activerecord_json_validator).

validates :settings, json: { schema: SETTINGS_SCHEMA }, allow_blank: true

SETTINGS_SCHEMA looks something like:

  SETTINGS_SCHEMA = {
    type: 'object',
    properties: {
      color1:  { type: 'string' },
      color2:  { type: 'string' },      
    },
    additionalProperties: false
  }.freeze

And you could create a schema validation in a .json file. This way, if you wanted to add new keys to your settings hash, you just update that file and deploy.

Also "type" (and also the key names) can be validated as a regular expression, so you could use a regex to validate that the value is a valid hexcode or something like that. No newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment