pyvalid icon indicating copy to clipboard operation
pyvalid copied to clipboard

Validation against the schema

Open eterey opened this issue 5 years ago • 8 comments

Provide an ability to validate objects against the schema

eterey avatar Oct 11 '20 23:10 eterey

Can you provide a brief explanation here?

nagesh-chowdaiah avatar Oct 17 '20 17:10 nagesh-chowdaiah

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.

eterey avatar Oct 18 '20 11:10 eterey

@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.

nagesh-chowdaiah avatar Oct 18 '20 11:10 nagesh-chowdaiah

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`
})

eterey avatar Oct 18 '20 12:10 eterey

@uzumaxy what should role=UserRole do?

nagesh-chowdaiah avatar Oct 18 '20 15:10 nagesh-chowdaiah

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 avatar Oct 18 '20 15:10 nagesh-chowdaiah

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 :)

eterey avatar Oct 19 '20 22:10 eterey

@uzumaxy Can you please check this

nagesh-chowdaiah avatar Oct 24 '20 12:10 nagesh-chowdaiah