Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
oECqg19h8DcWm4 opened this issue Feb 9, 2025 · 1 comment
Assignees
Labels
Type: Documentation Change Documentation needs some improvements

Comments

@oECqg19h8DcWm4
Copy link

oECqg19h8DcWm4 commented Feb 9, 2025

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;
      }
    }
  }  

@thetutlage
Copy link
Contributor

I think the docs have to be updated. The parse method is the first thing called before creating the field context. So it cannot be available.

Also, you should be using parse method to report errors, that is what the validation functions are meant for.

@thetutlage thetutlage self-assigned this Feb 26, 2025
@thetutlage thetutlage added the Type: Documentation Change Documentation needs some improvements label Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Documentation Change Documentation needs some improvements
Projects
None yet
Development

No branches or pull requests

2 participants