Ejemplo n.º 1
0
 internal static void EnsureValid(SchemaScope scope, JSchema schema, object value)
 {
     if (schema.Valid != null && !schema.Valid.Value)
     {
         scope.RaiseError($"Schema always fails validation.", ErrorType.Valid, schema, value, null);
     }
 }
Ejemplo n.º 2
0
        internal static bool TestType <T>(SchemaScope scope, JSchema currentSchema, JSchemaType currentType, T value)
        {
            if (!JSchemaTypeHelpers.HasFlag(currentSchema.Type, currentType))
            {
                scope.RaiseError($"Invalid type. Expected {currentSchema.Type.Value.GetDisplayText()} but got {currentType.GetDisplayText()}.", ErrorType.Type, currentSchema, value, null);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        internal static bool ValidateString(SchemaScope scope, JSchema schema, string value)
        {
            if (!TestType(scope, schema, JSchemaType.String, value))
            {
                return(false);
            }

            if (schema.MaximumLength != null || schema.MinimumLength != null)
            {
                // want to test the character length and ignore unicode surrogates
                StringInfo stringInfo = new StringInfo(value);
                int        textLength = stringInfo.LengthInTextElements;

                if (schema.MaximumLength != null && textLength > schema.MaximumLength)
                {
                    scope.RaiseError($"String '{value}' exceeds maximum length of {schema.MaximumLength}.", ErrorType.MaximumLength, schema, value, null);
                }

                if (schema.MinimumLength != null && textLength < schema.MinimumLength)
                {
                    scope.RaiseError($"String '{value}' is less than minimum length of {schema.MinimumLength}.", ErrorType.MinimumLength, schema, value, null);
                }
            }

            if (schema.Pattern != null)
            {
                if (schema.TryGetPatternRegex(
#if !(NET35 || NET40)
                        scope.Context.Validator.RegexMatchTimeout,
#endif
                        out Regex regex,
                        out string errorMessage))
                {
                    if (!RegexHelpers.IsMatch(regex, schema.Pattern, value))
                    {
                        scope.RaiseError($"String '{value}' does not match regex pattern '{schema.Pattern}'.", ErrorType.Pattern, schema, value, null);
                    }
                }
                else
                {
                    scope.RaiseError($"Could not validate string with regex pattern '{schema.Pattern}'. There was an error parsing the regex: {errorMessage}", ErrorType.Pattern, schema, value, null);
                }
            }
Ejemplo n.º 4
0
        internal static void EnsureValid(SchemaScope scope, JSchema schema, object value)
        {
            if (schema.Reference != null)
            {
                throw new JSchemaException("Schema has unresolved reference '{0}'. All references must be resolved before a schema can be validated.".FormatWith(CultureInfo.InvariantCulture, schema.Reference.OriginalString));
            }

            if (schema.Valid != null && !schema.Valid.Value)
            {
                scope.RaiseError($"Schema always fails validation.", ErrorType.Valid, schema, value, null);
            }
        }