Exemple #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);
        }
Exemple #2
-1
        public void PassTest()
        {
            Type ty;

            Assert.False(DynamicResolver.Find <Tag>("a", out ty));

            DynamicResolver.Register <Tag>("a", typeof(int));
            DynamicResolver.Register <Tag>("b", typeof(string));

            Assert.True(DynamicResolver.Find <Tag>("a", out ty));
            Assert.True(DynamicResolver.Find <Tag>("b", out ty));
            Assert.False(DynamicResolver.Find <Tag>("c", out ty));

            DynamicResolver.DeRegister <Tag>("b");

            Assert.True(DynamicResolver.Find <Tag>("a", out ty));
            Assert.False(DynamicResolver.Find <Tag>("b", out ty)); // "b" is unregisterd

            DynamicResolver.DeRegister <Tag>();

            Assert.False(DynamicResolver.Find <Tag>("a", out ty)); // All keys for Tag are unregisterd
        }