jamauro / method

An easy way to create Meteor methods
13 stars 1 forks source link

Error in validation step #10

Closed harry-73 closed 1 month ago

harry-73 commented 1 month ago

I have just tried to use the example in the README file but I get this error.

[
    {
        "name": "todos.create",
        "args": [
            {
                "text": "book flight to Hawaii"
            }
        ],
        "error": {
            "isClientSafe": true,
            "error": "validation-error",
            "reason": "Validation failed",
            "details": [
                {
                    "name": "done",
                    "type": "condition",
                    "message": "Done Unknown key"
                }
            ],
            "message": "Validation failed [validation-error]",
            "errorType": "Meteor.Error"
        }
    }
]
jamauro commented 1 month ago

How did you define the schema in createMethod? Any other info you can provide would be helpful. Thanks.

harry-73 commented 1 month ago

Sure, it is like that as in the README file:

import { createMethod } from 'meteor/jam:method'; // can import { Methods } from 'meteor/jam:method' instead and use Methods.create if you prefer

export const create = createMethod({
  name: 'todos.create',
  schema: Todos.schema, // using jam:easy-schema in this example
  async run({ text }) {
    const todo = {
      text,
      done: false,
      createdAt: new Date(),
      authorId: Meteor.userId(), // can also use this.userId instead of Meteor.userId()
    }
    const todoId = await Todos.insertAsync(todo);
    return todoId;
  }
});

And the schema is defined:

const schema = {text: String, isPrivate: Optional(Boolean)}

jamauro commented 1 month ago

The schema examples in the supported schemas section of the Readme are there just to illustrate syntax differences.

If you use jam:easy-schema, then your schema would need to look like this:

const schema = {
  _id: String,
  text: String, 
  done: Boolean,
  createdAt: Date,
  authorId: String,
}

This also assumes you're using Meteor-generated _ids (not Mongo.ObjectIds)

harry-73 commented 1 month ago

Sorry, I mixed up different things...

you can find a repo with the reproduction: https://github.com/harry-73/jam-method-meteor

I use jam:easy-schema package also.

I got the following error:

{
    "isClientSafe": true,
    "error": "validation-error",
    "reason": "Validation failed",
    "details": [
        {
            "name": "text",
            "type": "condition",
            "message": "Text Unknown key"
        }
    ],
    "message": "Validation failed [validation-error]",
    "errorType": "Meteor.Error"
}
jamauro commented 1 month ago

Thanks for the repo. You found an interesting issue when using vite. I've addressed it in jam:easy-schema v1.3.3.

harry-73 commented 1 month ago

I confirm, v1.3.3 fixes the issue.