Ich verdinge mich gerade an einem API Script und schaue mir dazu das Eve Api Framework an. Dieses basiert auf Flask.
In der Docu und dem Webinar von TalkPython wird folgendes beschrieben.
Code: Alles auswählen
from eve import Eve
import re
from eve.io.mongo import Validator
class MyValidator(Validator):
""" You can extend or override the built-in validation rules easily
by inheriting from the eve.io.mongo.Validator class
"""
def _validate_type_email(self, email, field, value):
""" Extend types by adding a new 'email' type """
if not re.match(r"[^@]+@[^@]+\.[^@]+", value):
if email:
self._error(field, 'Value is not a valid email address')
Code: Alles auswählen
{"lastname": "basasdasdasdls","email": "das@tholo.de"}
nur Leider kann ich den folgenden Fehler kaum nachvollziehen.
Code: Alles auswählen
{
"_status": "ERR",
"_issues": {
"exception": "_validate_type_email() missing 2 required positional arguments: 'field' and 'value'"
},
"_error": {
"code": 422,
"message": "Insertion failure: 1 document(s) contain(s) error(s)"
}
}
Code: Alles auswählen
people = {
# A class with the authorization logic for the endpoint. If not provided
# the eventual general purpose auth class will be used.
# 'authentication': MyBasicAuth,
# When False, this option disables HATEOAS for the resource.
# Defaults to True.
#'hateoas': False,
# A dict defining the actual data structure being handled by the resource.
# Enables data validation.
'schema': {
'firstname': {
'type': 'string',
'minlength': 1,
'maxlength': 30,
},
'lastname': {
'type': 'string',
'maxlength': 50,
'required': True,
'unique': True,
}
# dieses Regex funtioniert!
#'email': {'type': 'string',
# 'regex': '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
#------------
# this only works when app.MyValidator is used, as it brings support
# for validating a field of type 'email'
#Error in Api! "exception": "_validate_type_email() missing 2 required positional arguments: 'field' and 'value'"
'email': {'type': 'email'} # Irgendwas fehlt hier...
}
https://github.com/talkpython/eve-build ... pis-course erkenne aber keinen Unterschied. Auch die Doco von http://python-eve.org/] Eve und Cerebus haben mich nicht weiter gebracht.
Mir ist bewusst das die Klasse MyValidator und die Methode _validate_type_emai 2 Argumente braucht. "Field" und "Value" Field ist "Mail" aber wie soll ich den String aus dem Post Eintrag als "Value" übertragen?
Mein Request (Autogenerated) aus Postman sieht ungefähr so aus
Code: Alles auswählen
import requests
url = "http://127.0.0.1:5000/people"
payload = "{\n\t\"lastname\": \"basasdasdasdls\",\n\t\"email\": \"das@tholo.de\"\n\t\n}"
headers = {
'Content-Type': "application/json",
'Cache-Control': "no-cache",
'Postman-Token': "ec72d098-42e7-42ec-bd62-716eb672e34d"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
Könnt ihr mir verraten wo mein Hänger ist? Ich gehe von aus das ich in der Settings.py (in Github die __init__.py unter Domain) das Value im Dict irgendwie beschreiben muss. Oder?