Validation against the schema
Provide an ability to validate objects against the schema
Can you provide a brief explanation here?
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.
@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.
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`
})
@uzumaxy what should role=UserRole do?
Listing the possible in inputs values for Schema keys
- Object of Validator Class ( 'AbstractValidator', 'IterableValidator', 'NumberValidator', 'StringValidator', 'SchemaValidator')
- Built in data types
- List if Validators
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 :)
@uzumaxy Can you please check this