Giter Site home page Giter Site logo

Adding validators to fields about flywheel HOT 3 CLOSED

stevearc avatar stevearc commented on June 3, 2024
Adding validators to fields

from flywheel.

Comments (3)

numberoverzero avatar numberoverzero commented on June 3, 2024

Adding validators might be easier if Fields were implemented as Descriptors. Right now Models implement __getattribute__ and __setattribute to handle watching field changes, which is where that logic would probably live (or I guess, Field.resolve).

Using Descriptors would make it pretty easy to drop validation in, and it's nice for performance to remove __getattribute__. __getattr__'s overhead isn't nearly as bad, but Descriptors are somewhat designed for this task :)

If I can get the attribute_name change in #16 implemented, I should have enough context to pr a Descriptor change (and then setting validators on Fields will be straightforward).

from flywheel.

stevearc avatar stevearc commented on June 3, 2024

Actually, there's already support for validators! I should probably call it out more in the docs, but right now you can pass a function to the Field() constructor. The function should accept a value and, if the value is incorrect, throw an error. The validators are only called when saving to the database. For example:

class String(object):
    def __init__(self, max_length):
        self.max_length = max_length

    def __call__(self, val):
        if len(val) > self.max_length:
            raise ValueError("String %r exceeds maximum length!" % val)

And then to use it you could do:

class MyModel(flywheel.Model):
    username = Field(check=String(18))

from flywheel.

mortaliorchard avatar mortaliorchard commented on June 3, 2024

Thanks @stevearc / @numberoverzero for looking into this and taking the time to reply. Good to know the validators are already supported.

Issue resolved :)

from flywheel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.