Пример #1
0
        ConstraintsViolationException ValidateObjectField(string key,
                                                          object value,
                                                          State state,
                                                          JsonSchemaRegistory reg)
        {
            var matched = false;

            if (_schema._dynamicResolverTag != null)
            {
                Type dynElemType;
                if (DynamicResolver.Find(_schema._dynamicResolverTag, key, out dynElemType))
                {
                    var dynElemSchema = JsonSchemaAttribute.CreateFromType(dynElemType, reg, true);
                    var ex            = dynElemSchema.Validate(value, state, reg);

                    if (ex != null)
                    {
                        return(new ConstraintsViolationException("DynamicResolver", ex));
                    }
                }
            }

            if (_schema.Properties != null)
            {
                JsonSchemaAttribute itemSchema = null;
                if (_schema.Properties.TryGetValue(key, out itemSchema))
                {
                    matched = true;

                    var ex = itemSchema.Validate(value, state, reg);
                    if (ex != null)
                    {
                        return(new ConstraintsViolationException("Property", ex));
                    }
                }
            }

            if (_schema.PatternProperties != null)
            {
                foreach (var pprop in _schema.PatternProperties)
                {
                    if (Regex.IsMatch(key, pprop.Key))
                    {
                        matched = true;

                        var ex = pprop.Value.Validate(value, state, reg);
                        if (ex != null)
                        {
                            return(new ConstraintsViolationException("PatternProperties", ex));
                        }
                    }
                }
            }

            if (_schema.AdditionalProperties != null && !matched)
            {
                var ex = _schema.AdditionalProperties.Validate(value, state, reg);
                if (ex != null)
                {
                    return(new ConstraintsViolationException("AdditionalProperties", ex));
                }
            }

            return(null);
        }
Пример #2
0
        ConstraintsViolationException ValidateObject(object v, State state, JsonSchemaRegistory reg)
        {
            var validated = new Dictionary <string, object>();

            foreach (var kv in TypeHelper.ToKeyValues(v))
            {
                var ex = ValidateObjectField(kv.Key, kv.Value, state.NestAsElem(kv.Key), reg);
                if (ex != null)
                {
                    return(ex);
                }

                validated.Add(kv.Key, kv.Value);
            }

            if (_schema.Required != null)
            {
                var req = new HashSet <string>(_schema.Required);
                req.IntersectWith(validated.Keys);

                if (req.Count != _schema.Required.Count())
                {
                    var actual   = String.Join(", ", req.ToArray());
                    var expected = String.Join(", ", _schema.Required);
                    var msg      = state.CreateMessage("Lack of required fields(Actual: [{0}]; Expected: [{1}])",
                                                       actual, expected);
                    return(new ConstraintsViolationException(msg));
                }
            }

            if (_schema.MaxProperties != int.MinValue)
            {
                if (!(validated.Count <= _schema.MaxProperties))
                {
                    var msg = state.CreateMessage("MaxProperties assertion !({0} <= {1})",
                                                  validated.Count, _schema.MaxProperties);
                    return(new ConstraintsViolationException(msg));
                }
            }

            if (_schema.MinProperties != int.MaxValue)
            {
                if (!(validated.Count >= _schema.MinProperties))
                {
                    var msg = state.CreateMessage("MaxProperties assertion !({0} >= {1})",
                                                  validated.Count, _schema.MinProperties);
                    return(new ConstraintsViolationException(msg));
                }
            }

            if (_schema.Dependencies != null)
            {
                var strDep = _schema.Dependencies as Dictionary <string, string[]>;
                if (strDep != null)
                {
                    foreach (var va in validated)
                    {
                        string[] deps = null;
                        if (strDep.TryGetValue(va.Key, out deps))
                        {
                            var intersected = ((string[])deps.Clone()).Intersect(validated.Keys);
                            if (intersected.Count() != deps.Count())
                            {
                                var actual   = String.Join(", ", intersected.ToArray());
                                var expected = String.Join(", ", deps);
                                var msg      = state.CreateMessage("Dependencies assertion. Lack of depended fields for {0}(Actual: [{1}]; Expected: [{2}])",
                                                                   va.Key, actual, expected);
                                return(new ConstraintsViolationException(msg));
                            }
                        }
                    }
                    goto depChecked;
                }

                var schemaDep = _schema.Dependencies as Dictionary <string, JsonSchemaAttribute>;
                if (schemaDep != null)
                {
                    foreach (var va in validated)
                    {
                        JsonSchemaAttribute ext = null;
                        if (schemaDep.TryGetValue(va.Key, out ext))
                        {
                            var ex = ext.Validate(v, new State().NestAsElem(va.Key), reg);
                            if (ex != null)
                            {
                                // TODO:
                                var msg = state.CreateMessage("Dependencies assertion. Failed to validation for {0}",
                                                              va.Key);
                                return(new ConstraintsViolationException(msg, ex));
                            }
                        }
                    }
                }

depChecked:
                ;
            }

            return(null);
        }