diff --git a/ballerina/tests/constraint_annotation_test.bal b/ballerina/tests/constraint_annotation_test.bal index 62e34f2..ee01362 100644 --- a/ballerina/tests/constraint_annotation_test.bal +++ b/ballerina/tests/constraint_annotation_test.bal @@ -38,7 +38,7 @@ public type ValidationPerson record {| option: { value: "PAST", message: "Date of birth should be past value" - }, + }, message: "Invalid date found for date of birth" } time:Date dob; @@ -87,9 +87,9 @@ function testValidConstraintAnnotationForParseString() returns error? { test:assertEquals(person.dob.month, 12); test:assertEquals(person.dob.day, 31); test:assertEquals(person.family.id, 2221); - test:assertEquals(person.family.members.length(), 2); - test:assertEquals(person.family.members[0], "John"); - test:assertEquals(person.family.members[1], "Doe"); + test:assertEquals(person.family.members.length(), 2); + test:assertEquals(person.family.members[0], "John"); + test:assertEquals(person.family.members[1], "Doe"); } @constraint:Array { @@ -108,7 +108,7 @@ public type ValidationItem record {| function testInvalidConstraintAnnotationForParseString(string sourceData, typedesc expType, string expectedError) { anydata|Error err = parseString(sourceData, {}, expType); test:assertEquals(err is Error, true); - test:assertEquals(( err).message(), expectedError); + test:assertEquals((err).message(), expectedError); } function invalidConstraintAnnotation() returns [string, typedesc, string][] { @@ -196,7 +196,7 @@ function invalidConstraintAnnotation() returns [string, typedesc, str `, ValidationPerson, "Validation failed for '$.height:maxFractionDigits' constraint(s)." - ], + ], [ string ` @@ -254,6 +254,70 @@ function invalidConstraintAnnotation() returns [string, typedesc, str ]; } +@test:Config { + groups: ["constraint-validation"], + dataProvider: disableConstraintValidation +} +function testDisableConstraintValidationForParseString(string sourceData, typedesc expType, anydata result) returns error? { + anydata err = check parseString(sourceData, {enableConstraintValidation: false}, expType); + test:assertEquals(err, result); +} + +function disableConstraintValidation() returns [string, typedesc, anydata][] { + return [ + [ + string ` + + John Doe + 4 + 185.215 + + 5000 + 12 + 31 + + + 22213 + John + Doe + Ross + + + `, + ValidationPerson, + { + "name": "John Doe", + "age": 4, + "height": 185.215, + "dob": { + "year": 5000, + "month": 12, + "day": 31 + }, + "family": { + "id": 22213, + "members": [ + "John", + "Doe", + "Ross" + ] + } + } + ], + [ + string ` + + 1.2 + 2.3 + 3.4 + + `, + ValidationItem, + {"weight": [1.2, 2.3, 3.4]} + ] + ]; +} + @test:Config { groups: ["constraint-validation"] } @@ -283,9 +347,9 @@ function testValidConstraintAnnotationForParseAsType() returns error? { test:assertEquals(person.dob.year, 1990); test:assertEquals(person.dob.month, 12); test:assertEquals(person.dob.day, 31); - test:assertEquals(person.family.members.length(), 2); - test:assertEquals(person.family.members[0], "John"); - test:assertEquals(person.family.members[1], "Doe"); + test:assertEquals(person.family.members.length(), 2); + test:assertEquals(person.family.members[0], "John"); + test:assertEquals(person.family.members[1], "Doe"); } @test:Config { @@ -295,7 +359,7 @@ function testValidConstraintAnnotationForParseAsType() returns error? { function testInvalidConstraintAnnotationForParseAsType(xml sourceData, typedesc expType, string expectedError) { anydata|Error err = parseAsType(sourceData, {}, expType); test:assertEquals(err is Error, true); - test:assertEquals(( err).message(), expectedError); + test:assertEquals((err).message(), expectedError); } function invalidConstraintAnnotationForParseAsType() returns [xml, typedesc, string][] { @@ -383,7 +447,7 @@ function invalidConstraintAnnotationForParseAsType() returns [xml, typedesc @@ -440,3 +504,67 @@ function invalidConstraintAnnotationForParseAsType() returns [xml, typedesc expType, anydata result) returns error? { + anydata err = check parseAsType(sourceData, {enableConstraintValidation: false}, expType); + test:assertEquals(err, result); +} + +function disableConstraintValidationForParseAsType() returns [xml, typedesc, anydata][] { + return [ + [ + xml ` + + John Doe + 4 + 185.215 + + 5000 + 12 + 31 + + + 22213 + John + Doe + Ross + + + `, + ValidationPerson, + { + "name": "John Doe", + "age": 4, + "height": 185.215, + "dob": { + "year": 5000, + "month": 12, + "day": 31 + }, + "family": { + "id": 22213, + "members": [ + "John", + "Doe", + "Ross" + ] + } + } + ], + [ + xml ` + + 1.2 + 2.3 + 3.4 + + `, + ValidationItem, + {"weight": [1.2, 2.3, 3.4]} + ] + ]; +}