preaction / Yancy

The Best Web Framework Deserves the Best Content Management System
http://preaction.me/yancy/
Other
54 stars 21 forks source link

Missing property error while it is defined and required; no error if not required #97

Closed OpossumPetya closed 4 years ago

OpossumPetya commented 4 years ago

Standalone Yancy. SQLite DB. I simplified it to the minimum.

DB schema:

-- sqlite3 test.db < db-schema.sql
CREATE TABLE posts (
    id INTEGER PRIMARY KEY,
    dt DATETIME NOT NULL
);

yancy.conf:

{
    backend => 'sqlite:./test.db',
    editor => { require_user => undef },
    schema => {
        posts => {
            title => 'Posts',
            required => [ 'dt' ],
            'x-list-columns' => [ 'id', 'dt' ],
            properties => {
                id => {
                    type => 'integer',
                    readOnly => 1,
                },
                dt => {
                    type => 'string',
                    format => 'date-time',
                    default => 'now',
                },
            },
        },
    },
}

When dt is required, I receive following error in UI when adding new value

Error Adding Item
Error from server: Data validation failed

And this in console

... [error] Error validating new item in schema "posts": /dt: Missing property.
... [debug] [GWOQswAz] 400 Bad Request (0.006721s, 148.787/s)

If I remove dt from the required list, item is inserted successfully. I feel like I'm missing something maybe?

preaction commented 4 years ago

Oh, so, technically dt isn't required: If not provided, there will be a default value. One of the benefits of things like JSON schema and/or XML schema is that the client can validate the data it is about to send. So, if dt is required, it must send an object with a dt property, and, if the client supports format, the value must be an ISO8601 date/time. Even the empty string is technically invalid (which is why I have to delete properties with the empty string, which there might be a bug around that).

read_schema should automatically not set the field as required if it has a default value (otherwise that's a bug).

I'm definitely open to discussion as to whether this is the correct behavior or not, though I think it makes the most sense: Fields that have defaults aren't required to be provided by the user.

OpossumPetya commented 4 years ago

ah, understood. makes sense. thanks for the detailed explanation! I'll close this.