Answered by:
Data annotation how to let specific data?

Question
-
User-1104215994 posted
Hi,
I'm working on a Web API 2 project. Besides the requirement that some properties are required, some only can have specific values. ("000000007173", "000000007174", "000000007178", "000000007179", "000000006508", "000000006509", "000000007209", "000000007210", "2KBIM", "10KBIM")
Is there a way to do this?
[Required(ErrorMessage = "Please add Product Code to the request.")] [MaxLength(15, ErrorMessage = "The product code can not be more than 15 characters")] public string productCode { get; set; }
Saturday, October 5, 2019 4:38 PM
Answers
-
User475983607 posted
Thank you Fei Han. I just want these ones, "000000001570", "000000001571", "000000001572", "000000001573", "000000001574", "000000001575", "000000001582", "000000001583", "000000001585", "000000001586".
If I use this
[0]{8}\d{4}
, it does also get something like this "000000001560" which I don't want. I need to get exactly the ones I mentioned.Correct, the RegEx you designed and coded matches eight zeros followed by 4 digits [0-9]. If you want a specific 4 digits then design and write code to accomplish the task.
The following matches eight zeros followed by 1570 or 1571 or 1572.
^[0]{8}1570|1571|1572$
It is up to you to determine if RegEx fits your requirements. As suggested above storing configuration in a database might be a better solution of you want a data driven design. The design is up to you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 7, 2019 11:24 AM
All replies
-
User475983607 posted
Is there a way to do this?Of course, it requires a little problem solving. First, check if one of the existing data annotations can be used to accomplish the task. For example a RegEx expression might work. If the existing attributes do not solve your problem, you can build a custom data annotation. Most likely, you need to design and write a bit of code.
Saturday, October 5, 2019 4:49 PM -
User-1104215994 posted
I am trying but with <g class="gr_ gr_17 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="17" data-gr-id="17">regex</g> it seems not possible.
[0]{8}\d{4}
Sunday, October 6, 2019 11:24 AM -
User753101303 posted
Hi,
seems not possible.What?
For now what you want to control is a bit unclear. Do you want to check a new product code follows a pattern (but the "2KBIM" seems to not match previous code, you shouldn't create a reg exp based on samples but based on knowing exactly which constraints are applied on the product code).
Or it is supposed to be a product code that does exists in a database?
Sunday, October 6, 2019 11:42 AM -
User-1104215994 posted
I just want those codes ("000000001570", "000000001571", "000000001572", "000000001573", "000000001574", "000000001575", "000000001582", "000000001583", "000000001585", "000000001586"). Other than those, I will say undefined product code.
Sunday, October 6, 2019 1:22 PM -
User61956409 posted
Hi cenk1536,
I'm working on a Web API 2 project. Besides the requirement that some properties are required, some only can have specific values. ("000000007173", "000000007174", "000000007178", "000000007179", "000000006508", "000000006509", "000000007209", "000000007210", "2KBIM", "10KBIM")
Is there a way to do this?
If you want to specify that a data field value of productCode must match
[0]{8}\d{4}
, and also accept some input like 2KBIM and 10KBIM (end with "KBIM"), you can try:[Required(ErrorMessage = "Please add Product Code to the request.")] [MaxLength(15, ErrorMessage = "The product code can not be more than 15 characters")] [RegularExpression(@"^[0]{8}\d{4}$|(^\d{1,10}KBIM$)", ErrorMessage = "undefined product code.")] public string productCode { get; set; }
With Regards,
Fei Han
Monday, October 7, 2019 7:43 AM -
User-1104215994 posted
Thank you <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="7" data-gr-id="7">Fei</g> Han. I just want these ones, "000000001570", "000000001571", "000000001572", "000000001573", "000000001574", "000000001575", "000000001582", "000000001583", "000000001585", "000000001586".
If I use <g class="gr_ gr_51 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="51" data-gr-id="51">this </g>
[0]{8}\d{4}
<g class="gr_ gr_51 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" id="51" data-gr-id="51"> ,</g> it does also get something like this "000000001560" which I don't want. I need to get exactly the ones I mentioned.Monday, October 7, 2019 7:52 AM -
User753101303 posted
So you mean you just want to check if those values are found in a list (maybe stored in a db so that it could be easily updated) ? In short you just want to have an api that works on existing products ?
Depending on what your API you'll likely need to load data from a db so you could easily handled the case where you don't find any thing to load. You are using EF or ADO.NET ?
Edit: using DataAnnotations seems a wrong path. Or if you mean this is a user entered value and you want to let the user know immediately you could use Ajax or https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.remoteattribute?view=aspnet-mvc-5.2
Monday, October 7, 2019 8:21 AM -
User475983607 posted
Thank you Fei Han. I just want these ones, "000000001570", "000000001571", "000000001572", "000000001573", "000000001574", "000000001575", "000000001582", "000000001583", "000000001585", "000000001586".
If I use this
[0]{8}\d{4}
, it does also get something like this "000000001560" which I don't want. I need to get exactly the ones I mentioned.Correct, the RegEx you designed and coded matches eight zeros followed by 4 digits [0-9]. If you want a specific 4 digits then design and write code to accomplish the task.
The following matches eight zeros followed by 1570 or 1571 or 1572.
^[0]{8}1570|1571|1572$
It is up to you to determine if RegEx fits your requirements. As suggested above storing configuration in a database might be a better solution of you want a data driven design. The design is up to you.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 7, 2019 11:24 AM