Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Validation #318

Open
raderio opened this issue Dec 29, 2018 · 3 comments
Open

Add support for Validation #318

raderio opened this issue Dec 29, 2018 · 3 comments
Labels

Comments

@raderio
Copy link

raderio commented Dec 29, 2018

Usually on deserialization we want to collect all validation errors and return them in response to the client.
In order to catch all error the validation should be done before object creation.

If it will be done after object creation, in init block, than validation will be separated in 2 phases, first one will be validation regarding non-nullable field, because we cannot create objects if they have non-nullable properties.
The second one will be to do structural validation: min/max values, length, regexp pattern, etc.

Example
class Data(val name: String, val age: Int, val email: String)
when we send a json {"email": "xyz@xyz.com", "name": "A"} we should return a response where to indicate that age can not be null and name size must be greater than 2, at once to send all the errors.

Also, structural validation will be great to do based on annotations and not in init block, something like JSR 303.
This will be useful for documentation generation, it is far easier to generate based on annotations.

@raderio
Copy link
Author

raderio commented Jan 4, 2019

How I see the flow:

  1. Read annotations and generate validation rules. Can be done by 3rd party library. As result Map<Class, List>
  2. Parse json as a map
  3. Apply validation rules. Validation rules can be passed as parameter in configuration.
  4. Create object

@sandwwraith
Copy link
Member

Hm, with this flow I think it is possible to create a validation framework on top of kotlinx.serialization, because it provides both converters: json -> json tree and json tree -> object (see JsonTreeParser and JsonTreeMapper).

The Json in serialization itself does direct mapping without intermediate tree

@raderio
Copy link
Author

raderio commented Feb 22, 2019

For my understanding, validation and deserialization should be provided by same library, because deserialization module decides to which class to deserialize, in case of polymorphism for sure.
But validation should be done before desrilization, but to do validation we should know to which class will be done deserialization.

So, steps will be

  1. Determination to which class to deserialize
  2. Validation
  3. Deserialization

So, deserialization & validation are very tied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants