-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Parse method does not return a boolean #1066
Comments
Right, the docs are not matching the implementation. There is no sign of returning a boolean. It would make sense to return a boolean in the first place, however the thrown error contains the information about the parser error. If you would change the method in a way that it returns a bool and doesn't throw an exception, this would be a breaking change in the api which should be avoided. Therefore I don't see the point in returning a boolean. This is more like a flaw in the docs. |
I see what you mean, but having the ability to return both boolean and error info would be great. Maybe the return value can be an object instead? That way you can still easily implement simple checking, but still get detailed error info upon invalid markup. The object would probably be something like this, with a nullable error property :
For example:
|
Still the |
Hmm, then maybe this method shouldn't really be sold as a validation check, or the docs should say that you need to use a try catch to return what you want to return. That, or just make a separate method called |
You're right, I also consider that to be a flaw in the docs. I like the idea of a separate method, that could be handy. Will implement that. |
@klemmchr Do you have any update on this? Are you still able to work on a solution? |
Not really, I don't really have the spare time to help out here and I'm heavily out of sync with the codebase. |
Will this method be deprecated, until it is fixed? In the meantime, I will be adjusting the docs to and providing an alternative method of validation and also link to this issue for reference. If this issue does get corrected please update here: |
The upcoming parse in v10(#4108) will support both use cases. /**
* Parse the text and validate the syntax.
* @param text - The mermaid diagram definition.
* @param parseOptions - Options for parsing.
* @returns true if the diagram is valid, false otherwise if parseOptions.suppressErrors is true.
* @throws Error if the diagram is invalid and parseOptions.suppressErrors is false.
*/
async function parse(text: string, parseOptions?: ParseOptions): Promise<boolean | void> { |
I tried using |
Actually I think the return value of |
fix(#1066): Return true if parse is success.
Describe the bug
The
mermaid.parse()
method is supposed to return a boolean when passed a string of mermaid markdown, but it is instead returningundefined
for valid markup, and anUncaught
error on invalid markup.To Reproduce
mermaid.parse('graph TD\n')
= returnsundefined
mermaid.parse('graph TD\n{')
= returnsUncaught {str: "Parse error on line 1...
Expected behavior
Per this section of the current documentation,
mermaid.parse('graph TD\n')
= should returntrue
mermaid.parse('graph TD\n{')
= should returnfalse
Screenshots
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: