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

Validation against the schema #41

Open
eterey opened this issue Oct 11, 2020 · 8 comments
Open

Validation against the schema #41

eterey opened this issue Oct 11, 2020 · 8 comments
Milestone

Comments

@eterey
Copy link
Owner

eterey commented Oct 11, 2020

Provide an ability to validate objects against the schema

@nagesh-chowdaiah
Copy link

Can you provide a brief explanation here?

@eterey
Copy link
Owner Author

eterey commented Oct 18, 2020

Hello @nagesh-chowdaiah ,
I'm planning to provide an ability to validate the data against the schema. Here's a good example of schema-object mapping: https://mongoosejs.com/docs/guide.html

In most cases it will be useful for validating JSON data or any other kind of data in dictionaries.

@nagesh-chowdaiah
Copy link

@uzumaxy So say for example i have the input for a function as a dict/json and i need to validate each key input against the given schema?

Schema = {
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
any input given should match the given schema?
If yes this is a great idea which can be extended the dataframes as well. check if each column stores data of given schema type.

@eterey
Copy link
Owner Author

eterey commented Oct 18, 2020

Hey @nagesh-chowdaiah ,
Yep, you've got the idea :) I also think it would be nice to provide an ability to use built-in or custom pyvalid's validators for schemas. Something like that (just a concept):

from enum import enum
from pyvalid import accepts
from pyvalid.validators import Schema, StringValidator, NumberValidator
from myapp.models.address import address_schema


user_schema = Schema({
    'name': StringValidator(re_pattern=r'^[A-Za-z]+\s?[A-Za-z]+\s?[A-Za-z]+$'),
    'birthyear': NumberValidator(min_val=1890, max_val=2020),
    'address': address_schema,  # it should be possible to use one schema inside an another one
    'rating': float,  # let's still provide an ability use python types for data validation (if incoming value matches the specific data-type),
    'bio': [StringValidator(max_len=1024), None]
})


class UserRole(Enum):
    ADMIN: 1,
    MODERATOR: 2
    REGULAR_USER: 3


# So we can use schemas for `@accepts` and `@returns`
@accepts(new_user=user_schema, role=UserRole)
def register_user(new_user, role):
    # some logic here
    pass

# Or we can call them directly to validate some data
user_schema({
    'name': 'Max',
    'birthyear': -42, # will cause a validator error
    'bio': None,
    'rating': 8.0
    # the `address` attribute is missing, what will cause another error even if we fix `birthyear`
})

@nagesh-chowdaiah
Copy link

@uzumaxy what should role=UserRole do?

@nagesh-chowdaiah
Copy link

Listing the possible in inputs values for Schema keys

  1. Object of Validator Class ( 'AbstractValidator',
    'IterableValidator',
    'NumberValidator',
    'StringValidator',
    'SchemaValidator')
  2. Built in data types
  3. List if Validators

nagesh-chowdaiah pushed a commit to nagesh-kumar-abinbev/pyvalid that referenced this issue Oct 18, 2020
nagesh-chowdaiah added a commit to nagesh-kumar-abinbev/pyvalid that referenced this issue Oct 18, 2020
nagesh-chowdaiah pushed a commit to nagesh-kumar-abinbev/pyvalid that referenced this issue Oct 18, 2020
nagesh-chowdaiah added a commit to nagesh-kumar-abinbev/pyvalid that referenced this issue Oct 18, 2020
nagesh-chowdaiah added a commit to nagesh-kumar-abinbev/pyvalid that referenced this issue Oct 18, 2020
eterey added a commit that referenced this issue Oct 19, 2020
@eterey
Copy link
Owner Author

eterey commented Oct 19, 2020

Hey @nagesh-chowdaiah ,

what should role=UserRole do?

I just wanted to demonstrate that it shall be possible to valide the function with the multiple parameters and use Schema as one of the validators.

So you can just ignore it in your PR :)

nagesh-chowdaiah pushed a commit to nagesh-kumar-abinbev/pyvalid that referenced this issue Oct 21, 2020
@nagesh-chowdaiah
Copy link

@uzumaxy Can you please check this

eterey added a commit that referenced this issue Oct 24, 2020
@eterey eterey added this to the 1.2 milestone Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants