Giter Club home page Giter Club logo

Comments (4)

tomchristie avatar tomchristie commented on May 25, 2024

Right now the pattern attribute is strictly a string...

https://github.com/encode/typesystem/blob/master/typesystem/fields.py#L106

I'd be very happy to:

  • Accept a PR that switched that to allowing either a string or a regex.
  • Added a pattern_regex property on the String field, which is used to store the compiled pattern.
  • Switch the validation to use the compiled pattern_regex to perform the matching.

So I think __init__ would look something like this...

if pattern is None:
    self.patten is None
    self.pattern_regex is None
elif isinstance(pattern, str):
    self.pattern = pattern
    self.pattern_regex = re.compile(pattern)
elif isinstance(pattern, re.Pattern):
    self.pattern = pattern.pattern
    self.pattern_regex = pattern

from typesystem.

tomchristie avatar tomchristie commented on May 25, 2024

Similar to this we'll also want beter support for customizing error messages, so that eg...

# Match valid variable names like 'someFunctionName123'
variable_name = typesystem.String(
    pattern="^a-zA-Z_[a-zA-Z0-9_]*$",
    errors={"pattern": "Must be a valid variable name."}
)

But we'll handle that under #28

from typesystem.

wbolster avatar wbolster commented on May 25, 2024

i'll cook a pr. it's gonna be simpler than your suggestions.

from typesystem.

tomchristie avatar tomchristie commented on May 25, 2024

Sure thing 👍
However you implement, you'll want to make sure that the pattern attribute always gets set to a plain string - we inspect that attribute when generating JSON schema documents.

from typesystem.

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.