如何用JSON Schema验证键值未知的对象?含variables字段校验需求
Alright, let's walk through how to validate this specific JSON structure step by step, based on your requirements:
JSON Structure Validation Requirements
First, let's clarify all the rules we need to enforce:
- The top-level object does not require a
variablesfield (it's optional). - If the
variablesfield is included:- It must be an object with at least one key-value pair (empty objects are not allowed here).
- Keys can be any valid string (no restrictions on naming conventions).
- Values can be any data type (string, number, boolean, object, array) — but cannot be empty or null (this includes null values, empty strings, empty arrays, and empty objects; adjust this definition if your use case has different standards for "empty").
Valid JSON Examples
These structures pass all validation rules:
// Full valid example (matches your sample) {"variables": {"team": "red", "L3_HCU_TESTS": "N", "dbo_user": "user", "version": 920}} // Valid: No variables field (since it's optional) {} // Valid: Variables with mixed non-empty value types {"variables": {"is_enabled": true, "preferences": {"notifications": "on"}, "roles": ["editor", "moderator"]}}
Invalid JSON Examples
These structures fail validation for the reasons noted:
// Invalid: Variables is an empty object (needs at least one key-value pair) {"variables": {}} // Invalid: Variables contains a null value {"variables": {"status": null}} // Invalid: Variables contains an empty string {"variables": {"username": ""}} // Invalid: Variables contains an empty array {"variables": {"permissions": []}}
Automated Validation Options
If you need to automate this check, here are two common approaches:
1. JSON Schema (Standardized Validation)
A JSON Schema that enforces all the rules above looks like this:
{ "type": "object", "properties": { "variables": { "type": "object", "minProperties": 1, "additionalProperties": { "not": { "anyOf": [ {"type": "null"}, {"type": "string", "maxLength": 0}, {"type": "array", "maxItems": 0}, {"type": "object", "maxProperties": 0} ] } } } }, "additionalProperties": true }
This schema ensures:
variablesis optional.- If present,
variableshas at least one property. - No value in
variablesis null, empty string, empty array, or empty object.
2. Code Example (JavaScript)
Here's a simple function to validate the structure in JavaScript:
function validateTargetJson(obj) { // Check if variables exists in the top-level object if ('variables' in obj) { const variables = obj.variables; // Variables must be a non-null object if (typeof variables !== 'object' || variables === null) { return false; } // Variables must have at least one key-value pair if (Object.keys(variables).length === 0) { return false; } // Check each value is non-empty (customize this logic as needed) for (const value of Object.values(variables)) { const isEmpty = value === null || (typeof value === 'string' && value.trim() === '') || (Array.isArray(value) && value.length === 0) || (typeof value === 'object' && value !== null && Object.keys(value).length === 0); if (isEmpty) { return false; } } } // All checks passed return true; }
Feel free to modify the isEmpty logic to match your exact definition of what counts as an invalid empty value.
内容的提问来源于stack exchange,提问作者MAAS
相关产品推荐
相关产品推荐

