Skip to content

Question - When using the Parse function to parse input, the second argument should be FieldContext, but it is not #42

@oECqg19h8DcWm4

Description

@oECqg19h8DcWm4

I am using the Parse function to parse a JSON string input into a JSON object before validating. Per the docs, the Parse function takes a callback with the value to be parsed as the first argument and the FieldContext as the second: https://vinejs.dev/docs/schema_101#parsing-input-value

When a valid JSON string is inputted, the parse function return JSON as expected. However, when attempting to report an error from inside the parse callback by using FieldContext.report, it reveals that the second argument is not an instance of FieldContext because the Report function is not available.

How can I report an error from inside the Parse function?

  static tags = vine.array(vine.string().minLength(1).maxLength(64)).parse(this.jsonParse);

  static jsonParse(value, field) {
    console.log(test);
    if(typeof(value) == "object") {
      return value;
    } else {
      try {
        return JSON.parse(value);
      } catch(error) {
          field.report(
            "The {{ field.name }} field is not valid JSON",
            "JSON",
            field
          );
        return null;
      }
    }
  }  

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions