Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Verbose validation error message#296

Merged
yuhr merged 6 commits intoruntypes:masterfrom
HzrdIRL:verbose-validation-error-message
Aug 11, 2022
Merged

Verbose validation error message#296
yuhr merged 6 commits intoruntypes:masterfrom
HzrdIRL:verbose-validation-error-message

Conversation

@HzrdIRL
Copy link
Contributor

@HzrdIRL HzrdIRL commented Jan 19, 2022

This PR addresses #260 - validation error messages should surface the incorrect fields without requiring extra error handling.

Previously:
Expected [number, { name: string; }], but was incompatible

Now:

Validation failed:
[
  {
    "name": "Expected string, but was number"
  }
].
Object should match [number, { name: string; }]

@d33vil
Copy link

d33vil commented May 18, 2022

Any traction on this PR?

@HzrdIRL
Copy link
Contributor Author

HzrdIRL commented Jun 9, 2022

I would also like to know if this will get merged 😞

@OmgImAlexis
Copy link
Contributor

I've been using this personally because of how bad the error is.

import { ValidationError } from 'runtypes';
import type { RuntypeBase } from 'runtypes/lib/runtype';

export const validateRunType = <T = unknown>(runType: RuntypeBase, data: T): T => {
	try {
		return runType.check(data) as T;
	} catch (error: unknown) {
		if ((error as ValidationError).details) throw new Error(JSON.stringify((error as ValidationError).details));
		throw error;
	}
};

@yuhr
Copy link
Member

yuhr commented Aug 11, 2022

I'm very sorry for being unresponsible for a long time. Now I've got some time to work here.

@yuhr yuhr force-pushed the verbose-validation-error-message branch from 9332157 to ca71bcd Compare August 11, 2022 11:45
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

Successfully merging this pull request may close these issues.

Unuseful error message on typechecking Better info to explain where object doesn't match the RunType

4 participants