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

Render schema validation errors #138

Open
Fer0x opened this issue Jul 11, 2022 · 0 comments
Open

Render schema validation errors #138

Fer0x opened this issue Jul 11, 2022 · 0 comments

Comments

@Fer0x
Copy link
Contributor

Fer0x commented Jul 11, 2022

This is copy of internal issue created before autoviews was migrated to external github project

For discussion:
If onError callback blocks render then no need to fix different handlers for unmatched schema.

Right now we are ignoring error without onError handler.
We still can make use of this callback, but we also can make some run-time decisions when we know that that AutoView in so called "error" state.
In order to do this, we need couple of things

  1. Save error in validate method https://github.com/wix-incubator/autoviews/blob/master/packages/core/src/auto-view/auto-view.tsx#L170-L172
if (!isValid && validate.errors!.length) {
    this.error = formatValidationError(validate.errors![0]);
    if (onError) {
        onError(error)
    }
}
  1. propagate error to child AutoViews https://github.com/wix-incubator/autoviews/blob/master/packages/core/src/auto-view/auto-view.tsx#L154-L160
return (
    <AutoViewLogic  
        {...this.props}
        error={this.error}
        schema={subSchema!}
        validation={false}  
    />
);
  1. Create error component in repository
const repo = new ComponentsRepo();

repo.registerError({
    name: 'error-component',
    predicate: props => {
        return props.error.schemaPointer === props.schemaPointer
    },
    component: props => (
         <>
              <h2
                   children={props.data.message}
              />
              <pre
                    style={{backgroundColor: red}}
                    children={JSON.stringify(props.error.data)}
              />
        </>
    )
})
  1. call error component before any logic https://github.com/wix-incubator/autoviews/blob/master/packages/core/src/auto-view/auto-view.tsx#L85
const error = this.error || this.props.error;
if (error) {
     const ErrorComponent = components.getErrorComponen(this.props);
     if (ErrorComponent) {
          return <ErrorComponent {...this.props} error={error}/>
     }
}

(this will probably require to move validation to constructor and also more complex logic for wrappers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant