Keystone-classic: New field type: JSON

Created on 24 Feb 2014  路  12Comments  路  Source: keystonejs/keystone-classic

Would store a JSON string in the database, and have a nice renderer / editor in the Admin UI.

Could use pretty-stringify for formatting the JSON, and Code Mirror for editing.

Would validate JSON on save.

Most helpful comment

You can use "code" field.

Sample:

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true },
    password: { type: Types.Password, initial: true, required: false },
.....
    configuration: { type: Types.Code, lang: 'json' }
);

If language is json or javascript, show lint validator.

Supported languages:

  • c
  • c++
  • objetivec
  • css
  • asp
  • c#
  • vb
  • xml
  • php
  • html
  • ini
  • js
  • java
  • coffee
  • lisp
  • perl
  • python
  • sql
  • json
  • less
  • sass
  • sh
  • ruby
  • jsp
  • tpl
  • jade

All 12 comments

Would be nice if it would take dirty JSON and clean it up:

input

{
  myKey: "My Value",
  "anotherKey": 'another value'
}

output

{"myKey": "My Value", "anotherKey": "Another Value"}

Personally I get so tired of having to drop to the console or node.js paste in perfectly valid code and then use JSON.stringify(obj) to get out "Valid" JSON.

You can use "code" field.

Sample:

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true },
    password: { type: Types.Password, initial: true, required: false },
.....
    configuration: { type: Types.Code, lang: 'json' }
);

If language is json or javascript, show lint validator.

Supported languages:

  • c
  • c++
  • objetivec
  • css
  • asp
  • c#
  • vb
  • xml
  • php
  • html
  • ini
  • js
  • java
  • coffee
  • lisp
  • perl
  • python
  • sql
  • json
  • less
  • sass
  • sh
  • ruby
  • jsp
  • tpl
  • jade

problem with using "Types.Code" is that it will store the JSON as text. Meaning you will not be able to run queries against it. I really wish you can throw in raw objects. This is supported by mongodb, mongoose by default, why is this option not available?

@uptownhr someone has to code it. There's probably no technical reason blocking this.

I created a field type Types.Json that store in mongo a JSON in raw format. I commit two pull requests to branch 0.3.x and master.

Hi I have published my npm package:
https://github.com/kadosh1000/keystone-custom-fieldtypes

It allows you to create custom field types, and copy your custom fieldTypes from a selected dir into the keystone's fields dir and registers it.

We're closing all feature requests to keep the issue tracker unpolluted. From now on submit feature requests on productpains.com!

This worked for me and allowed my to save an arbitrary object, only it's not visible in the Admin UI because it is unsupported. But you can query the fields just fine.

list.schema.add({
    model: mongoose.Schema.Types.Mixed
});

@mxstbr @JedWatson i have make a pull request on latest version base on . @jbalde
code. https://github.com/keystonejs/keystone/pull/4855

since this has been around for 3 years, is there a suggested work around for existing data schema with Mixed types (responses from third parties etc)

trying out keystone v5 and was hoping to hit the ground running with a light cms around my data

    transcription_job: { type: JSON, schemaDoc: 'response of transcription job' }

going with Text for now and hoping it renders ok in gets

@victusfate
Not a great solution, but you could just save JSON as plain test - perhaps in a Textarea type - and after reading it in from mongo in your controller run JSON.parse on it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kamontat picture kamontat  路  5Comments

celiao picture celiao  路  4Comments

Twansparant picture Twansparant  路  5Comments

sorryididntmeantto picture sorryididntmeantto  路  3Comments

ttsirkia picture ttsirkia  路  4Comments