如何在RAML中针对books数组参数的特定值组合返回400 Bad Request错误?
Can RAML Enforce This Validation Logic?
Yes, you absolutely can implement this specific validation rule with RAML—though you’ll need to leverage JSON Schema constraints since basic RAML type definitions don’t natively support cross-value array validation like this.
Here’s how to structure it in RAML 1.0:
#%RAML 1.0 title: Book Catalog API types: ValidBooksArray: type: array items: enum: [a, b, c, d] # Embed JSON Schema to block co-occurrence of b and c (schema): not: allOf: - contains: const: b - contains: const: c
How This Works:
- The core RAML type defines the allowed enum values for the array items, just as you specified.
- The
(schema)annotation lets you inject raw JSON Schema rules into the RAML type. This is the key to adding the custom validation. - The JSON Schema logic uses
not+allOf+containsto reject any array that includes bothbandc:allOf: [contains b, contains c]targets arrays that have both values- Wrapping that in
notmeans "reject any array that matches this condition"
When a request sends a books array like [a,b,c], [b,c], or [b,c,d], the validation will fail, and you can configure your RAML tooling to return a 400 Bad Request error as required.
Most modern RAML tools (like MuleSoft Anypoint Platform, or RAML parsers that support JSON Schema integration) will recognize and enforce this constraint correctly.
内容的提问来源于stack exchange,提问作者SH4444KO
相关产品推荐
相关产品推荐

