Is there a way to pre-define built-in validators that can be used multiple times?

Đang mở
#2,829 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
35/100
Loại issue
Tài liệu
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu với tài liệu marshmallow.validate được liên kết và các entry point hiện có của validator. Xác định xem các validator tích hợp có thể tái sử dụng đã được hỗ trợ hay chưa, đồng thời ghi lại một ví dụ phù hợp về cách áp dụng một validator trên nhiều schema; công việc được xem là hoàn tất khi pattern được khuyến nghị đã rõ ràng trong tài liệu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

question

I'm working on an API based flask app with flask-smorest and marshmallow validations. The issue I'm running into, is that many api endpoints require the same type of data, and I don't want to continually copy/paste the same validations over and over. Something like below would happen....

from marshmallow import Schema, fields, validate

class api1():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate.Regexp(regex=r"^REQUEST\d{3,9}$", error="Input string didn't match required format - REQUEST12345"))	
		
class api2():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate.Regexp(regex=r"^REQUEST\d{3,9}$", error="Input string didn't match required format - REQUEST12345"))		

What I want to do is create some sort of function or class BEFORE I define every api field/schema that I could recall.

Below is the idea, though I don't believe it works.

from marshmallow import Schema, fields, validate

class repetative_validators():
	request_number = validate.Regexp(regex=r"^REQUEST\d{3,9}$", error="Input string didn't match required format - REQUEST12345"))

class api1():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=repetative_validators().request_number)
		
class api2():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=repetative_validators().request_number)	

I was able to successfully test creating my own function for validation as described here, but was hoping to use the built in validators.

from marshmallow import Schema, fields, validate, ValidationError
import re

def validate_request_number(value):
	if not re.match(r"^REQUEST\d{3,9}$"):
		raise ValidationError("Input string didn't match required format - REQUEST12345")
		
class api1():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate_request_number)
		
class api2():

	class input1(Schema):
		request_number = fields.Str(metadata={'description': 'Request Number', 'example':'REQUEST12345'}, validate=validate_request_number)		

If anyone can link to appropriate documentation or an example that would be helpful. I haven't found anything like this in the marshmallow docs yet

Ngôn ngữ chính
Python
Star
7.2k
Fork
790
Merge trung bình
10 giờ 40 phút
Pull request đã merge (30 ngày)
6

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của marshmallow-code/marshmallow

Tất cả issue của marshmallow-code/marshmallow

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.