bartoszlenar / Validot

Validot is a performance-first, compact library for advanced model validation. Using a simple declarative fluent interface, it efficiently handles classes, structs, nested members, collections, nullables, plus any relation or combination of them. It also supports translations, custom logic extensions with tests, and DI containers.
MIT License
311 stars 19 forks source link

AsType scope command - validate as different type #3

Closed bartoszlenar closed 2 years ago

bartoszlenar commented 4 years ago

Feature description

Feature in action

Full custom option:

Specification<CustomType> customTypeSpecification = s => s;

TryConvertDelegate<int, CustomType> intToCustomType = (int input, out CustomType output) =>
{
    if (input < 0)
    {
        output = null;
        return false;
    }

    output = new CustomType(input);

    return true;
};

Specification<int> specification = s => s
    .AsType<CustomType>(customTypeSpecification)
    .WithConversion(intToCustomType) // might be required for custom conversion
    .WithConversionErrorMessage("Conversion error!") // optional, if not present, message would be taken from dict;

Predefined options:

Specification<int> specification = s => s
    .AsString(stringSpecification);

Specification<string> specification = s => s
    .AsInt(intSpecification)
    .WithConversionErrorMessage("Invalid string as int!");

Feature details

tim-fay commented 2 years ago

Hello @bartoszlenar, I'd like to know if there's any pans of adding this feature? Thank you a lot for your work on this library!

bartoszlenar commented 2 years ago

Hi @tim-fay. Sorry for the late reply, I'm on my holidays until the end of month.

I haven't abandoned the project, but the pace slowed down significantly lately. Partially because I don't know what other developers really want from this lib. And now I do, thanks to you.

Thank you for adding your comment and please expect this feature... well... this year for sure. Alongside other features I'm slowly (but consistently!) adding to the codebase.

bartoszlenar commented 2 years ago

@tim-fay I'm on the task and so far, this is the progress with the planning:

Please expect the feature sooner rather than later 😄

bartoszlenar commented 2 years ago

The final version for this api is:

.AsConverted(converted, specification).

bartoszlenar commented 2 years ago

Merged to the main branch with this commit: https://github.com/bartoszlenar/Validot/commit/d379a9912e8df5b02217e51410634a2140afbc09

tim-fay commented 2 years ago

Merged to the main branch with this commit: d379a99

Thanks so much, @bartoszlenar ! Really appreciate you for this PR and the whole project!