-
Notifications
You must be signed in to change notification settings - Fork 7
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
Comments
Can you provide a brief explanation here? |
Hello @nagesh-chowdaiah , 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 = { |
Hey @nagesh-chowdaiah , 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
|
#41 Pyvalid: Implemented SchemaValidator
Hey @nagesh-chowdaiah ,
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 |
Provide an ability to validate objects against the schema
The text was updated successfully, but these errors were encountered: