I'm using the module ejs for validating ejs template and variables that come from the admin dashboard:
try {
resolve(ejs.render(template, variables))
} catch (error) {
reject(error)
}
And if there are some issues with this template, it throws an error, and I can send it to the client.
Also, I have a mustache template to validate. I'm using the same approach:
try {
resolve(mustache.render(template, variables))
} catch (error) {
reject(error)
}
But mustache doesn't throw an error at all, and if there is no variable, it just replaces it with an empty string.
How can I solve this issue? Need to validate templates properly.
I have found a solution in this fork of the library.
Thanks to Scott it works like should.
If you're using mustache.js, there is no way to have it throw errors on undefined variables, here is an open issue. This seems to be fine according to the Mustache specification.
By default a variable "miss" returns an empty string. This can usually be configured in your Mustache library.
So if you really need to validate that there are no undefined variables, you're going to have to use another JS library for Mustache (if there is any, I'm not sure), or hope that they add the feature.
For anyone still needing a solution for this and doesn't want to/can't use another templatinh library I've created a small package to add data validation to Mustache: https://www.npmjs.com/package/mustache-validator
Related
I have C# razor *.cshtml template file and would like to use it for Angular (5) server side rendering.
C# .cshtml snippet:
#helper RenderLookupComponent()
{
<list-lookup
[data]="getLookupData('#Model.LookupSourceType')"
[lookupType]="'#Model.LookupSourceType'">
</list-lookup>
}
Everything works fine until '#Model.LookupSourceType' value is not contains escape symbol ('\'), for e.g. when #Model.LookupSourceType = 'SomeType\'
Angular template parser Error:
[error]: Template parse errors:
Parser Error: Missing expected ) at column # in [getLookupData('SomeType\')]
At first glance, it looks like ordinary issue, actually it can be solve by replacing all '\' to '\'. (#Model.LookupSourceType.Replace(#"\", #"\\")).
Q: But i believe there should be more elegant, universal approaches to solve such kind of problems.
The approach that can handles all possible scenario for different kind of escape symbols
Please share your experience
Problem was solved with using HttpUtility.JavaScriptStringEncode(Model.LookupSourceType)
I have a lot of JavaScript code that get's executed on a website. If every plugin works, everything is fine. But when only one JavaScript error occurs, all other code fails, too.
I am concatenating all JavaScript code to one file. This is currently a must, so a change here is not possible.
What's the general way of preventing (vendor) code not fail everything else?
EDIT:
To simplify my question: What's the best way of handling big JavaScript error from vendor code?
Your error handling should be as simple as the following example:
try{
var x = functionThatDoesNotExist();
}
catch(error){
console.log('An error occured');
console.log(error);
}
You should try and break down your javascript files according to the templates that contain your HTML markup, and simply when an error occurs do not render that template.
You can read more on Try/Catch here.
Suppose, I am writing a library with Typescript. There is a function with following signature-
function check(value: "YES"|"NO"): boolean
So, when this function is called from other typescript files with values other than "YES" or "NO", there will be a compilation error. But if called from a Javascript file, there will be no error, as Javascript do not has the type information. I can check for invalid values inside my function and throw errors. But then the type safety provided by Typescript seems only an illusion to me.
What should I do in this case as a library developer? Go with pure javascript? What did the teams like Angular do?
This is the difference between build-time (compile-time) and run-time error checking. TypeScript only helps you with Build-time checks. Providing a TypeScript Definition File with your library will help you users get meaningful compile-time errors when they use your lib incorrectly.
If your lib is being consumed by JavaScript directly, you'll have no build-step to notify the user, and you'll have to resort to run-time messaging. If file size is not an issue, I'd suggest throwing a meaningful error message:
function (check) {
if (check != "YES" && check != "NO")
throw new Error("Invalid Check Value: " + check);
...
}
If you are concerned about file size, probably best to simply no-op on invalid calls. Or have some sort of sensible default. It will depend on your situation.
You could also consider a "debug" build of your library that provides error messages, but exclude them from the minified release build.
i have a textarea in my project where users can save SQL queries but one of the requirements is to check if this query is valid or not
EXAMPLE
if user entered something like :
SELECT ** FROM EMP
this should return false and an error message just saying invalid it doesn't have to give any reasons
mainly the queries will be just select statement
NOTICE
i DON'T want to use any server side at this point
Question
Does any one knows a javascript / jquery library or plugin that have this functionality or any thing similar
i have Google this and it didn't show any thing
Thanks
Here is an example fiddle using the JS SQL Parser:
http://jsfiddle.net/Hb6az/
The parser will throw an error if it hits something unexpected, so you have to wrap your checking code in a try {} catch(error) {} block.
I've been using JSONLint online which has been really helpful. I NEED to get this into Node.js though.
Our QA and even I put in so much invalid JSON it's not even funny. The server is using the connect bodyDecoder from connect which currently barfs on invalid input (not sure why the stock bodyDecoder doesn't even catch the error, but oh well). I'll probably want to make my own bodyDecoder, but I can only find a C based JSONLinter.
Is there any library I can use in Node.js to throw helpful errors (such as on http://www.jsonlint.com/) instead of crazy "string not expected" vague errors?
Pure javascript implementation: https://github.com/zaach/jsonlint, common js compatible to boot.