funkybob / gilbert

A simple static site generator.
https://gilbert.readthedocs.io/en/latest/
MIT License
10 stars 4 forks source link

Validating datetimes #8

Closed shuttle1987 closed 5 years ago

shuttle1987 commented 5 years ago

When I attempt to use a datetime I run into issues with validation:

plugins.py

import typing
from datetime import datetime

from gilbert.content import Templated, Content

class BuzzwordSummary(Templated, Content):
    term: str
    posted: datetime = datetime.now()
    template: str = "buzzword.html"

Example page:

content_type: BuzzwordSummary
term: low hanging fruit
---
A way of describing a task that's easy to do

This results in the following traceback:

  File "/Users/janis/BuzzwordsGlossary/buzzwords_glossary/plugins.py", line 6, in <module>
    class BuzzwordSummary(Templated, Content):
  File "/Users/janis/BuzzwordsGlossary/venv/lib/python3.7/site-packages/gilbert/schema.py", line 149, in __new__
    _type = validator_for(_type)
  File "/Users/janis/BuzzwordsGlossary/venv/lib/python3.7/site-packages/gilbert/schema.py", line 101, in validator_for
    raise TypeError(f"How do I validate a {_type !r} ?")
TypeError: How do I validate a <class 'datetime.datetime'> ?

How would we go about validating these?

funkybob commented 5 years ago

The recent change to schema means any type not explicitly know in validation will us a simple InstanceValidator, so this issue should now be resolved.