ServiceStack Validation/Serialization - servicestack

I have an application built on ServiceStack and razor (no MVC). The application has a class with an integer field. When I enter an integer with thousand separator(comma) from a web page that allows me to enter data for the field, I get an error with something like "'23,586' is an Invalid value for 'Fieldxx'".
Is there a way to allow integers with thousand separator?
Thanks.

No integers are whole numbers, if you want to keep using an integer I'd recommend your Web Page strip any formatting before sending the API request otherwise change it to a string and convert it into an int inside the Service implementation.

Related

How to setup Solr search across REST urls

We are implementing an API Portal and have a field named basePath to hold the base part of the api's rest url. Currently the field is defined as a string mapped to solr.StrField but we have search problems with this.
The problem right now is that in order to find an API by the basePath, we need double quote the value in the search. For example name:"/v1/api-proxy/generator" We cannot use name:/v1/api-proxy/* to see other apis that might have clashing urls. We know we have other urls like '/v1/api-proxy/validator' but something like name:/v1/api-proxy/* doesn't return any hits.
I am guessing a first step is to change away from 'string' to text or text_general, but how can search and find other hits that closely match the provided basePath?

asp.net mvc System.ComponentModel.DataAnnotations.EmailAttribute inconsistent validation client vs. server

On an asp.net mvc 5.2.4 model I have an email address field decorated thusly, with the System.ComponentModel.DataAnnotations.EmailAddressAttribute:
[DataType(DataType.EmailAddress)]
[EmailAddress(ErrorMessageResourceName = "EmailField", ErrorMessageResourceType = typeof(Messages))]
public string EmailAddress { get; set; }
It renders html that looks like this, an input of type "email":
<input type="email".../>
If I enter an email address without a full domain (eg "tom#yahoo") it passes client side validation, but it fails server-side validation. I only have to worry about 2 browsers (Chrome and IE 11), and it passes both of them client-side.
I think I understand what is happening. jquery.validate.js, the client-side validation, is following the international standard, which allows for emails without domains. The server-side is using a RegEx expression as long as my arm (see https://referencesource.microsoft.com/#System.ComponentModel.DataAnnotations/DataAnnotations/EmailAddressAttribute.cs) and throwing a validation error.
I'm guessing I'm not the first person to bump into this. Is there an attribute I can decorate an email address field with that corresponds exactly to input type="email" in the browser, so that if an email address passes client-side validation it will always pass server-side validation?
You could write your own validator. You already know the regex that MS is using and if you feel it is the more accurate/correct for your needs then you can "reuse" (in quotes as you will likely have to format it differently) the regex in your client side validator. This requires that you create your own ValidationAttribute for server side validation & to wire up to the client side you'll implement the IClientValidatable interface & add a method on the js validator.
You have a leg up in that you don't have to write it from scratch either. You can use the source code for the EmailAddressAttribute to get you started.
http://ezzylearning.com/tutorial/creating-custom-validation-attribute-in-asp-net-mvc has a nice writeup.
MVC custom validation: compare two dates has another example of doing both server & client side with comparing two dates but the idea of what to implement & how is the same.
My workaround is to remove the EmailAddress attribute, copy the regular expression used by client-side validation (jquery.validate.js v1.16.0 line 1356) and use that in a RegularExpression validation attribute, so my code now looks like this:
[DataType(DataType.EmailAddress)]
[RegularExpression(EmailRegEx, ErrorMessageResourceName = "EmailField", ErrorMessageResourceType = typeof(Messages))]
Where "EmailRegEx" is a constant with this value:
^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$

Trim string in JSF

I am building a Web application using JSF.
I have the following line in my code:
where the value is being fetched from properties file.
Suppose if the value of that particular key changes in the properties file,I don't want this value to be displayed with increased length.
Rather I need to display original string and append it with "..."
How can I achieve this?

Using Hashids to protect the id in database

People get the data from url like http://domain/api/user/1 and the 1 is the id(primary key) in the database. We want to avoid this to prevent people detect the ids.
Then I found Hashids which can encode/decode the id to string, the url may change to http://domain/api/user/D42dhDLdk
It is a wonderful solution at the first glance, however how to use it in the real application?
1 encode
When records are retrieved from the database, we should encode the id to string, which is the right place to do this job? The only right place I can thought is the deserialization of the records, for example, when you serve data of json, you can encode the related ids to string, but you have to make sure which fields should be encoded.
For example:
{user:{id:1,name:'xx',projects:[{id:1,name:'p1',id:2,name:'p2'}]},icon_id:1}}
The id of the user project and the icon_id should be encoded, there are no clear rules to abstract that.
2 decode
You have to decode the string to id when retrieve the data from db, I think we can do this in the route or controller layer, however there are still no clear rules to make a abstract middleware to hanlde this, since the encoded string may exist in the url or the query params.
I have not find any general solution from google, so I wonder how do you handle this kind of problem?
Update:
I am using Mysql, and Express with sequelzie as the ORM tool.

Web API - GET - passing in multiple parameters in this scenario

When I have this URI and pass in the PlayerCode: 12345, everything is good.
https://abc.com/teams/players/12345
But when I have a list of 9000 player codes how do I pass the specific list of order code list for a GET operation?
While this question -asked before,here - suggests "an" answer I am not sure if it is "the" answer. I am not sure if I should be going for something like :
https://abc.com/teams/players/?PlayerCodes=12345,23456,34567,45678....
and then have custom model binders to cater to the above.
Does passing in 9000 comma separated values in a URI make sense?
What would be the optimal solution for this scenario?
unfortunately, when you get into the realm of big numbers like 9000, query string parameters will not be sufficient. I assume you are running your solution in IIS or IIS express, both of which have character limits on the query string of somewhere around 2048. In this scenario you can either choose to do an HTTP POST and post a body of the playerId's for the players you need to retrieve, or you could rework your architecture a bit and break your GET calls up into acceptable sizes.

Resources