question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question. Validating nested collections and proceeding only when everything went successfully.

See original GitHub issue

This is a cross post from SO. I’ve got the following DTOs:

public class DatumDto
{
    public int Year { get; set; }
    public decimal Value { get; set; }
}
public class UpdateDataDto
{
    public int Id { get; set; }
    public IEnumerable<DatumDto> TimeSeries { get; set; }
}

And the following domain classes.

public class Datum
{
    public int Year { get; }
    public decimal Value { get; }

    public static Validation<Error, Datum> Of (int year, decimal value)
    {
        // ... logic

        return new Datum(year, value)
    }
}
public class Data
{
    public int Id { get; }
    public IEnumerable<Datum> TimeSeries { get; }

    public Validation<Error, Data> UpdateTimeSeries(IEnumerable<Datum> timeSeries)
    {
        // ... logic
    }
}

I’ve stripped some code out to have less clutter and focus on what matters to me.

Now my use case is very simple. I need to load Data with TimeSeries entities from the database and then execute a command against them, otherwise return all errors to the client.

My command looks as follows:

public class UpdateDataCommand
{
    public Validation<Error, int> Execute(IEnumerable<UpdateDataDto> arguments)
    {
        var data = _repository.Get(arguments.Map(s => s.Id));

        // part I am struggling at
        // before making any actual changes, I have to validate whether ALL DatumDtos ar valid Datums
        // If all of them are valid within all arguments - I should proceed, otherwise gather all faulty Dtos and bring them back.

        // I've come here, but it does not compile

        var result =
            from d in data
            join arg in arguments on d.Id equals arg.Id
            from datum in arg.TimeSeries
            from validation in Domain.Datum.Of(datum.Year, datum.Value)
            select d.UpdateTimeSeries(validation);   // this does not compile
    }
}

How would I go about validation whether all DatumDtos are valid within each element in arguments collection and only proceeding when everything went fine and gather all errors if something has gone wrong?

I understand this might be something trivial, but it’s not yet clicking to me. I’d appreciate help.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
TysonMNcommented, Oct 15, 2019

…I wasn’t sure if this is going to reach anyone on SO, given that language-ext tag has very few questions…

I think a better metric is how many people are watching and who is watching the language-ext tag. I think @louthy watches that tag. I used to watch it but no longer do 😢 <— (tear of joy) I think you would have gotten an answer.

While @louthy typed you his reply, I typed up this code for you. Like he said, since your example isn’t functional, I am not exactly sure what you want. Nonetheless, here is some code that uses Sequence and Seq<> just like @louthy recommended.

public class UpdateDataCommand
{
    public Validation<Error, IEnumerable<Data>> Validate(
        IEnumerable<UpdateDataDto> arguments
    ) => arguments
        .Map(Validate)
        .Sequence();

    public Validation<Error, Data> Validate(
        UpdateDataDto argument
    ) => argument
        .TimeSeries
        .Map(d => Datum.Of(d.Year, d.Value))
        .Sequence()
        .Bind(ds => Data.UpdateTimeSeries(argument.Id, ds));
}

public class DatumDto
{
    public int Year { get; set; }
    public decimal Value { get; set; }
}

public class UpdateDataDto
{
    public int Id { get; set; }
    public Seq<DatumDto> TimeSeries { get; set; }
}

public class Datum
{
    public int Year { get; }
    public decimal Value { get; }

    private Datum(int year, decimal value)
    {
        Year = year;
        Value = value;
    }

    public static Validation<Error, Datum> Of(int year, decimal value)
    {
        // ... logic
        return new Datum(year, value);
    }
}

public class Data
{
    public int Id { get; }
    public Seq<Datum> TimeSeries { get; }

    private Data(int id, Seq<Datum> timeSeries)
    {
        Id = id;
        TimeSeries = timeSeries;
    }

    public static Validation<Error, Data> UpdateTimeSeries(int id, Seq<Datum> timeSeries)
    {
        // ... logic
        return new Data(id, timeSeries);
    }
}
0reactions
TysonMNcommented, Oct 17, 2019

I managed to implement everything I needed. … I appreciate all the help, thank you @bender2k14 and @louthy.

Wonderful! Great job.

Your welcome 😃

In case I have any questions in the future, providing a failing test case with expectations as in previous comment would work?

A failing test case is perfect. Good luck with your future coding!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javax validation on nested objects - not working
If I use the @Valid annotation on the building property, it would validate all of its fields, but for this case I only...
Read more >
Nested cross validation for model selection
How do I choose a model from this [outer cross validation] output? Short answer: You don't. Treat the inner cross validation as part...
Read more >
Deep nested validation broken · Issue #614
Description When trying to validate nested objects using @ValidateNested(), whitelist: true and forbidNonWhitelisted: true, error "property ...
Read more >
Problem with nested object validation · Issue #306
The problem is with class-transformer - you declare an object type, you provide an array and it fails on transform.
Read more >
Nested Cross-Validation for Machine Learning with Python
A procedure is required that allows both the models to select well-performing hyperparameters for the dataset and select among a collection ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found