RyanSquared / gigaspoon

A library for making Flask forms easier to deal with.
MIT License
1 stars 0 forks source link

change @sb.validator to take a name argument #1

Open RyanSquared opened 6 years ago

RyanSquared commented 6 years ago

for example, change:

@sb.validator(RegexValidator("username", "[A-Za-z]+"))
@sb.validator(CustomUsernameValidator("username"))

to look like:

@sb.validator("username", RegexValidator("[A-Za-z]+"), CustomUsernameValidator())

reasoning

with something like SQLAlchemy, we have the following:

from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import validates
import spudbucket as sb
from .your_app import email_validator

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    email = Column(String)

    @validates("email")
    @sb.sqlalchemy_validator("email", email_validator)
    def validate_email(self, key, field):
        # Do your more-"advanced" validation here
        return field

the reason we don't assign name to the validator is that the SQL and the form might have two different values.

RyanSquared commented 6 years ago

this would also probably mean having:

# add-to
class Validator():
    def set_field_name(name):
        self._name = name

so the name is still accessible and can still be set, but not required for each validator

RyanSquared commented 6 years ago

will need to be done after tests due to incompatibility with current code in tests branch

RyanSquared commented 5 years ago

this would also mean we'd need something like @sb.flask_validator("email", email_validator), so that we can actually use this for Flask forms.

this keeps it detached entirely from the names.

alternatively:

import gigaspoon.flask

@gigaspoon.flask.validator({
    "email": [email_validator]
})
def validate_email(self, key, field):
    return field
RyanSquared commented 5 years ago

Progress made on eb6bc35.

This provides the Flask integration which can be used for sanitizing form inputs and preparing forms.