Beispiel #1
0
        /**
         * Asserts that the string represents a valid JsonPath expression.
         *
         * @param path         Path expression to validate.
         * @param propertyName Name of property.
         */
        public void AssertIsValidJsonPath(string path, string propertyName)
        {
            if (path == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(path))
            {
                ProblemReporter.Report(new Problem(this, $"{propertyName} cannot be empty"));
            }

            // Newtonsoft.Json seems to allow JsonPath to not start with $
            if (path[0] != '$')
            {
                ProblemReporter.Report(new Problem(this,
                                                   $"{propertyName} with value '{path}' is not a valid JsonPath. Must Start with '$'"));
            }

            try
            {
                var token = JToken.Parse("{}").SelectTokens(path);
            }
            catch (Exception e)
            {
                ProblemReporter.Report(new Problem(this,
                                                   $"{propertyName} with value '{path}' is not a valid JsonPath. {e.Message}"));
            }
        }
Beispiel #2
0
 /**
  * Asserts the value is not null, reporting to {@link ProblemReporter} with this context if it is.
  *
  * @param propertyValue Value to Assert on.
  * @param propertyName  Name of property.
  */
 public void AssertNotNull(object propertyValue, string propertyName)
 {
     if (propertyValue == null)
     {
         ProblemReporter.Report(new Problem(this, $"{propertyName} is a required property."));
     }
 }
Beispiel #3
0
 /**
  * Asserts the integer is either null or non-negative, reporting to {@link ProblemReporter} with this context if it is.
  *
  * @param integer      Value to Assert on.
  * @param propertyName Name of property.
  */
 public void AssertIsNotNegativeIfPresent(int?integer, string propertyName)
 {
     if (integer.HasValue && integer < 0)
     {
         ProblemReporter.Report(new Problem(this, $"{propertyName} must be non negative"));
     }
 }
Beispiel #4
0
 /**
  * Asserts the string is not null and not empty, reporting to {@link ProblemReporter} with this context if it is.
  *
  * @param propertyValue Value to Assert on.
  * @param propertyName  Name of property.
  */
 public void AssertStringNotEmpty(string propertyValue, string propertyName)
 {
     if (string.IsNullOrEmpty(propertyValue))
     {
         ProblemReporter.Report(new Problem(this, $"{propertyName} is a required property."));
     }
 }
Beispiel #5
0
 /**
  * Asserts the map is not null and not empty, reporting to {@link ProblemReporter} with this context if it is.
  *
  * @param map          Map to Assert on.
  * @param propertyName Name of property.
  */
 public void AssertNotEmpty(IDictionary map, string propertyName)
 {
     if (map == null || map.Count == 0)
     {
         ProblemReporter.Report(new Problem(this,
                                            $"{propertyName} requires one or more entries"));
     }
 }
Beispiel #6
0
 /**
  * Asserts the collection is not null and not empty, reporting to {@link ProblemReporter} with this context if it is.
  *
  * @param collection   Collection to Assert on.
  * @param propertyName Name of property.
  */
 public void AssertNotEmpty(IEnumerable collection, string propertyName)
 {
     if (!collection.GetEnumerator().MoveNext())
     {
         ProblemReporter.Report(new Problem(this,
                                            $"{propertyName} requires one or more items"));
     }
 }
Beispiel #7
0
        /**
         * Asserts that the string represents a valid reference path expression.
         *
         * @param path         Path expression to validate.
         * @param propertyName Name of property.
         */
        public void AssertIsValidReferencePath(string path, string propertyName)
        {
            if (path == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(path))
            {
                ProblemReporter.Report(new Problem(this, $"{propertyName} cannot be empty"));
            }

            try
            {
                var r = ReferencePath.Parse(path);
            }
            catch (InvalidReferencePathException e)
            {
                ProblemReporter.Report(new Problem(this,
                                                   $"{propertyName} with value '{path}' is not a valid ReferencePath. {e.Message}"));
            }
        }
Beispiel #8
0
 /**
  * Sets the problem reporter to report problems in Assertion methods.
  *
  * @return This object for method chaining.
  */
 public Builder ProblemReporter(ProblemReporter problemReporter)
 {
     _problemReporter = problemReporter;
     return(this);
 }
 internal StateValidationVisitor(Dictionary <string, State> states, ValidationContext context)
 {
     _states          = states;
     _currentContext  = context;
     _problemReporter = context.ProblemReporter;
 }