spiral-project / daybed

Form validation and data storage API
http://daybed.rtfd.org/
BSD 3-Clause "New" or "Revised" License
53 stars 8 forks source link

Storing a validating a List of items #189

Closed Natim closed 9 years ago

Natim commented 9 years ago

I'd like to be able to store and validate something like so:

[
  {"id": 1, "type": "text", "title": "This is a Text entry", "text": "Hello World."},
  {"id": 2, "type": "text", "title": "Lorem ipsum dolor sit amet",
   "text": "**Consectetur adipisicing elit**, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."},
  {"id": 3, "type": "todo", "title": "Today", "tasks": [
    {"label": "Walk the dog", "done": true},
    {"label": "Clean the car", "done": false},
    {"label": "Buy a new carpet", "done": false}
  ]},
  {"id": 4, "type": "text", "title": "Ut enim ad minim veniam",
   "text": "Quis nostrud *exercitation ullamco* laboris nisi ut aliquip ex ea commodo consequat."},
  {"id": 5, "type": "text", "title": "Duis aute irure",
   "text": "> Dolor in [reprehenderit](http://google.com/) in voluptate velit esse cillum dolore eu fugiat nulla pariatur."},
  {"id": 6, "type": "text", "title": "Excepteur sint occaecat",
   "text": "    Cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."},
  {"id": 7, "type": "todo", "title": "Trip to Paris", "tasks": [
    {"label": "Eat cheese", "done": false},
    {"label": "Eat more cheese", "done": false},
    {"label": "See Eiffel Tower", "done": false},
    {"label": "MOAR CHEESE", "done": false}
  ]}
]

In other words the definition it'd like would looks like this:

{
    "title": "kept",
    "description": "A list of items I need to keep track on",
    "fields": [
        {
            "name": "title",
            "type": "string",
            "label": "Title"
        },
        {
            "name": "type",
            "type": "enum",
            "label": "Type",
            "choices": ["text", "todo", "picture", "video"]
        },
        {
            "name": "tasks",
            "type": "list",
            "definition": {
                {
                  "name": "label",
                  "type": "string",
                  "label": "Label"
                },
                {
                    "name": "done",
                    "type": "boolean",
                    "label": "Done"
                }
            },
            "label": "Tasks",
            "linkedTo": {"type": "todo"}
        },
        {
            "name": "text",
            "type": "string",
            "label": "Text",
            "linkedTo": {"type": "text"}
        }
     ]
}

NB: By default a list type without definition should be a list of strings.

Natim commented 9 years ago

For the linkedTo conditionnal validation feature, let see if we want to add it right now or use two documents at the moment.

For this ticket I will focus on list of docs.

Natim commented 9 years ago

Is this already the group field type?

Natim commented 9 years ago
{"definition": {
    "title": "Keep — Todo",
    "description": "Store user text kept card.",
    "fields": [
      {
        "name": "title",
        "type": "string",
        "label": "Title"
      },
      {
        "name": "tasks",
        "type": "group",
        "label": "Tasks",
        "description": "List of todo list's tasks.",
        "fields": [
          {
            "name": "label",
            "type": "string",
            "label": "Label"
          },
          {
            "name": "done",
            "type": "boolean",
            "label": "Check when done!"
          }
        ]
      }
    ]
  }
}

This works, also this doesn't:

{
  "title": "Today",
  "tasks": [
    {"label": "Walk the dog", "done": true},
    {"label": "Clean the car", "done": false},
    {"label": "Buy a new carpet", "done": false}
  ]
}
Natim commented 9 years ago

groups is the way to go.

leplatrem commented 9 years ago

Interesting : groups are not relevant for what you describe (see list field and its test suite).

But I realize that the groups test suite is the only one that does not test records validation! Which leads me to think that the validation is not performed properly and let you save the records you described above...