pip install eve python from eve import Eve from cerberus import Validator app = Eve() schema = { 'name': { 'type': 'string', 'required': True, 'minlength': 2, 'maxlength': 50 }, 'age': { 'type': 'integer', 'required': True, 'min': 0, 'max': 120 }, 'email': { 'type': 'string', 'required': True, 'regex': '[\w\.-]+@[\w\.-]+' } } validator = Validator(schema) @app.route('/people', methods=['POST']) def add_person(): data = app.data.driver.request.get_json() if not validator.validate(data): return {'error': validator.errors}, 400 data['name'] = data['name'].capitalize() inserted_id = app.data.driver.db['people'].insert(data) return {'message': 'Person added successfully', 'inserted_id': inserted_id} if __name__ == '__main__': app.run()


上一篇:
下一篇:
切换中文