Validate ignored fields
See original GitHub issueMotivation
Sometimes in a flat file, there are fields that are supposed to have constant values.
You can just Ignored()
them but maybe for additional safety, you want to check that they contain the expected value and that the file is completely valid.
Current solutions The only solution I can think of today is mapping them as normal fields and having code that throws if they aren’t what you’d expect.
mapper.Property(x => x.Version);
class Row
{
public string Version
{
set { if (value != "4") throw new BadDataException(); }
}
}
Drawbacks
- When you have multiple constant fields, it’s annoying to have
Constant1
,Constant2
,Constant3
in your model, just like it’s annoying to declareIgnored1
,Ignored2
,Ignored3
. - A good exception to throw would be
ColumnProcessingException
and include context position. This exception isinternal
(so unavail. to user code) and the context is harder to get.
Proposal
Add a new Ignored(string value)
overload to Ignored(Window w)
.
This new overload would work like Ignored(value.Length)
when the file is correct, but when the ignored field is different from value
then it goes through the ColumnError
process (column event, then row event, then global exception).
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
jQuery validation & ignore field - javascript
The Unobtrusive plugin calls validate() when the document loads, which sets the options to defaults. A validator will then be created and cached ......
Read more >[jQuery] [validate] Ignore hidden form fields from the ...
I am currently hiding or showing specific form fields depending onspecific conditions. Only if the field is hidden they are still usedto validate...
Read more >Ignore field if not pass validation
Ignore field if not pass validation. Hy, i have one index route that returns all orders, and i implemented some filters from url...
Read more >How do I ignore validation for a single field #4070
I would like to ignore validation only for certain fields. I'd still like to be able to assign a value to and have...
Read more >Ignore some field from VarienForm validate
I have a form, and it is validated by: var productAddToCartForm = new VarienForm('product_addtocart_form');.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
OnParsing
occurs before I try to interpret the text from the file, so before I check if it isnull
and try to convert it to the column type (int
,double
,DateTime
, etc.). TheOnParsed
happens after I have determined it isnull
or converted it. In your case, since it’s for an ignored column, you can just useOnParsing
and look at the raw value – I will have replaced it withnull
by the timeOnParsed
fires: https://github.com/jehugaleahsa/FlatFiles/blob/master/FlatFiles/IgnoredColumn.cs#L51As for the exception, even something as simple as
throw new Exception("Unexpected ignored column token")
will cause the exception to get caught here: https://github.com/jehugaleahsa/FlatFiles/blob/master/FlatFiles/Schema.cs#L120. However, you may wish to use your own exception subclass if it helps you to distinguish between this specific error and something else, or you could simply look at the message contents.Again, when you give this a try, if you see anything unexpected or need more guidance, just let me know.
Yeah, curiously I had marked the
Preprocess
getter/setter on theIColumnDefinition
interface asObsolete
but that did not result in any warnings on theColumnDefinition
class that implemented that interface, nor did it cause any using code to complain. With 4.9.0, I added theObsolete
onto theColumnDefinition
and the warnings showed up. I also went through each of the property mapping classes and marked thePreprocess
methods asObsolete
, as well.The property mapping classes (and ignored mapping) now have four new methods for registering
OnParsing
,OnParsed
,OnFormatting
andOnFormatted
callbacks. I did have to modify theIgnoredColumn
to fire these events whereas before I just returnednull
and calledNullFormatter.FormatNull
(I am not even sure I did that much, honestly).This was actually a good exercise because it made me realize there were cases where these callbacks were not firing (when
null
s were being handled) and it also made me realize that in theSchema
class I was just skipping over ignored columns entirely, resulting innull
s getting written out instead of the desired value. 😬Please give 4.9.0 a shot and let me know if you are able to handle everything you wanted to do in your original comment above. If you run into any issues or still cannot figure out how to do something, just ping me and I’ll see what I can do. Otherwise, please close the ticket.