Browse Source

add oneOf validation attribute to json string validation

Kolja Strohm 2 years ago
parent
commit
df5ebbf6fd
2 changed files with 36 additions and 10 deletions
  1. 25 8
      JSON.cpp
  2. 11 2
      JSON.h

+ 25 - 8
JSON.cpp

@@ -1167,19 +1167,19 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
-			else if (zConstraints->hasAttribute("lessOrEqual") && zValue->asNumber()->getNumber() > (double)zConstraints->getAttributeValue("lessOrEqual"))
+			if (zConstraints->hasAttribute("lessOrEqual") && zValue->asNumber()->getNumber() > (double)zConstraints->getAttributeValue("lessOrEqual"))
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
-			else if (zConstraints->hasAttribute("greaterOrEqual") && zValue->asNumber()->getNumber() < (double)zConstraints->getAttributeValue("greaterOrEqual"))
+			if (zConstraints->hasAttribute("greaterOrEqual") && zValue->asNumber()->getNumber() < (double)zConstraints->getAttributeValue("greaterOrEqual"))
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
-			else if (zConstraints->hasAttribute("less") && zValue->asNumber()->getNumber() >= (double)zConstraints->getAttributeValue("less"))
+			if (zConstraints->hasAttribute("less") && zValue->asNumber()->getNumber() >= (double)zConstraints->getAttributeValue("less"))
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
-			else if (zConstraints->hasAttribute("greater") && zValue->asNumber()->getNumber() <= (double)zConstraints->getAttributeValue("greater"))
+			if (zConstraints->hasAttribute("greater") && zValue->asNumber()->getNumber() <= (double)zConstraints->getAttributeValue("greater"))
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
@@ -1196,18 +1196,36 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
-			else if (zConstraints->hasAttribute("contains") && zValue->asString()->getString().hat(zConstraints->getAttributeValue("contains").getText()))
+			if (zConstraints->hasAttribute("contains") && zValue->asString()->getString().hat(zConstraints->getAttributeValue("contains").getText()))
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
-			else if (zConstraints->hasAttribute("startsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("startsWith").getText()) == 0)
+			if (zConstraints->hasAttribute("startsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("startsWith").getText()) == 0)
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
-			else if (zConstraints->hasAttribute("endsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("endsWith").getText()) == zValue->asString()->getString().getLength() - zConstraints->getAttributeValue("endsWith").getLength())
+			if (zConstraints->hasAttribute("endsWith") && zValue->asString()->getString().positionVon(zConstraints->getAttributeValue("endsWith").getText()) == zValue->asString()->getString().getLength() - zConstraints->getAttributeValue("endsWith").getLength())
 			{
 				return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
 			}
+			if (zConstraints->hasAttribute("oneOf"))
+			{
+				JSONArray* array = Parser::getValue(zConstraints->getAttributeValue("oneOf"))->asArray();
+				bool ok = 0;
+				for (JSONValue* v : *array)
+				{
+					if (v->asString()->getString().istGleich(zValue->asString()->getString()))
+					{
+						ok = 1;
+						break;
+					}
+				}
+				array->release();
+				if (!ok)
+				{
+					return new JSONTypeMissmatch(path, dynamic_cast<JSONValue*>(zValue->getThis()), dynamic_cast<XML::Element*>(zConstraints->getThis()), 0);
+				}
+			}
 		}
 		break;
 	case JSONType::ARRAY:
@@ -1253,7 +1271,6 @@ JSONValidationResult* JSONValidator::validate(JSONValue* zValue, XML::Element* z
 				}
 				else
 				{
-					bool isValueValid = 0;
 					JSONValidationResult* res = validateMultipleTypes(zValue->asObject()->zValue(i.val()), zConstraints->selectChildsByAttribute("name", i.val()).begin().val(), p);
 					if (!res->isValid())
 					{

+ 11 - 2
JSON.h

@@ -432,6 +432,15 @@ namespace Framework
 					return this;
 				}
 
+				StringValidationBuilder<T>* whichIsOneOf(RCArray<Text>& values)
+				{
+					JSONArray arr;
+					for (Text str : values)
+						arr.addValue(new JSONString(str));
+					element.setAttribute("oneOf", arr.toString());
+					return this;
+				}
+
 				StringValidationBuilder<T>* withDefault(Text value)
 				{
 					element.setAttribute("default", JSONString(value).toString());
@@ -696,8 +705,8 @@ namespace Framework
 
 				ObjectValidationBuilder<T>* withDefault(JSONObject* obj)
 				{
-					element.setAttribute("default", obj->toString()); obj
-						obj->release();
+					element.setAttribute("default", obj->toString());
+					obj->release();
 					return this;
 				}