/**
         * Compares to JSchemas to see if they are the same.
         */
        private bool CompareSchemas(JSchema one, JSchema two)
        {
            if(one.Type != two.Type)
                return false;

            if(one.Type == JSchemaType.Object) {
                if (one.Properties.Count() != two.Properties.Count())
                    return false;

                foreach (var entry in one.Properties) {
                    if(!two.Properties.ContainsKey(entry.Key))
                        return false;

                    if (!CompareSchemas(entry.Value, two.Properties[entry.Key]))
                        return false;
                }
            }

            if(one.Type == JSchemaType.Array) {
                if(one.Items.Count() != two.Items.Count())
                    return false;

                for (var i = 0; i < one.Items.Count(); i++) {
                    if (!CompareSchemas(one.Items[i], two.Items[i]))
                        return false;
                }
            }

            // Types and subproperties are not different, and thus the same
            return true;
        }
		public void TestReportTitleConflictWhenBothHaveTitle()
		{
			var schema1 = new JSchema
			{
				Title = "s1"
			};
			var schema2 = new JSchema
			{
				Title = "s2"
			};

			var underTest = new SchemaCombiner();
			var report = new SchemaComparisionReport();
			var result = underTest.Apply(schema1, schema2, report);

			var failures = report.GetFailures().ToList();

			Assert.AreEqual(1, failures.Count, "One failure expected");
			Assert.IsInstanceOfType(failures[0], typeof(ValueConflict));

			var valueConflict = (ValueConflict)failures[0];
			Assert.AreEqual("title", valueConflict.PropertyName);
			Assert.AreEqual(schema1, valueConflict.SchemaPart);
			Assert.AreEqual(schema2, valueConflict.SchemaBasePart);
			Assert.AreEqual("s1", valueConflict.Value1);
			Assert.AreEqual("s2", valueConflict.Value2);
		}
        /**
         * Create a type given an object
         * 
         * Note that this does not handle additionalProperties, which behave like dictionaries.
         */
        protected Type ObjectToType(string typeSignature, JSchema schema)
        {
            TypeBuilder typeBuilder = moduleBuilder.DefineType(typeSignature,
                TypeAttributes.Public | TypeAttributes.Class |
                TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout,
                null);

            ConstructorBuilder constructor = typeBuilder.DefineDefaultConstructor(
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);

            // Add all properties
            foreach(KeyValuePair<string, JSchema> property in schema.Properties)
            {
                string propertyTypeSignature = typeSignature;

                if (typeSignature != "")
                    propertyTypeSignature += ".";

                propertyTypeSignature += property.Key;

                // Convert property to type
                Type propertyType = SchemaToType(propertyTypeSignature, property.Value);

                // Add property to object
                CreateProperty(
                    typeBuilder,
                    property.Key,
                    propertyType);
            }

            Type objectType = typeBuilder.CreateType();
            return objectType;

        }
		public void TestCombineSubSchemaProperties()
        {
            var schema1 = new JSchema();
            var propSchema1 = new JSchema();
            propSchema1.Properties.Add("sub_prop1", new JSchema());
            schema1.Properties.Add("prop1", propSchema1);

            var schema2 = new JSchema();
            var propSchema2 = new JSchema();
            propSchema2.Properties.Add("sub_prop2", new JSchema());
            schema2.Properties.Add("prop1", propSchema2);

            var underTest = new SchemaCombiner();
            var result = underTest.Apply(schema1, schema2, null);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Properties);
            Assert.AreEqual(1, result.Properties.Count);
            Assert.IsTrue(result.Properties.ContainsKey("prop1"), "Expect the property from both schema.");

            var innerProperties = result.Properties["prop1"].Properties;
            Assert.AreEqual(2, innerProperties.Count);
            Assert.IsTrue(innerProperties.ContainsKey("sub_prop1"), "Expect the property from the first schema.");
            Assert.IsTrue(innerProperties.ContainsKey("sub_prop2"), "Expect the property from the second schema.");
        }
        private EdmEntityType ConvertToEdmEntityType(JSchema schema, EdmModel model, string name)
        {
            var type = new EdmEntityType("namespace", name);

            foreach (var property in schema.Properties)
            {
                if (property.Value.Type == null)
                    throw new Exception("Type specyfication missing.");

                var structuralType = MapPropertyToStructuralType(property, schema, model);

                if (structuralType != null)
                {
                    type.AddStructuralProperty(property.Key, structuralType);
                }
                else
                {
                    type.AddStructuralProperty(property.Key, ToEdmPrimitiveType(property.Value.Type.Value));
                }
            }

            model.AddElement(type);

            return type;
        }
		public void TestUseCombinedSkinsSchemaWhenNoDefaultTemplate()
		{
			var templateInfo = new StringTemplateInfo("test", "");
			var templateInfo2 = new StringTemplateInfo("test2", "");
			var schema1 = new JSchema();
            var schema2 = new JSchema();

			var templateSchemaProvider = new Mock<ISchemaProvider>();
			templateSchemaProvider.Setup(f => f.GetSchemaFromTemplateAsync(templateInfo)).Returns(Task.FromResult(schema1));
			templateSchemaProvider.Setup(f => f.GetSchemaFromTemplateAsync(templateInfo2)).Returns(Task.FromResult(schema2));

			var combiner = new Mock<SchemaCombiner>();
			combiner.Setup(c =>
                c.Apply(It.Is<JSchema>(s => s == schema1), It.Is<JSchema>(s => s == schema2), It.IsAny<SchemaComparisionReport>(), It.IsAny<string>()))
                .Returns(new JSchema());

			var moduleDefintion = new ModuleDefinition("testmod", null, new Dictionary<string, TemplateInfo>
            {
				{"skin1", templateInfo},
                {"skin2", templateInfo2}
            });

			var underTest = new DefaultModuleSchemaProvider(combiner.Object, templateSchemaProvider.Object);

			var result = underTest.GetSchemaFromModuleAsync(moduleDefintion);

			Assert.IsNotNull(result);

			combiner.Verify();
		}
Exemple #7
0
        public string GetNamespaceName(JSchema schema)
		{
			var input = NormalizeName(schema.Title);

			string namespaceName;
			ExtractClassName(input, out namespaceName);
			return namespaceName;
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalSchema"/> class with the specified <see cref="JSchema"/>.
        /// </summary>
        /// <param name="schema">The schema.</param>
        public ExternalSchema(JSchema schema)
        {
            if (schema == null)
                throw new ArgumentNullException("schema");

            _uri = schema.Id ?? new Uri(string.Empty, UriKind.RelativeOrAbsolute);
            _schema = schema;
        }
        /// <summary>
        /// Determines whether the <see cref="JToken"/> is valid.
        /// </summary>
        /// <param name="source">The source <see cref="JToken"/> to test.</param>
        /// <param name="schema">The schema to test with.</param>
        /// <param name="errors">When this method returns, contains any errors generated while validating.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="JToken"/> is valid; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValid(this JToken source, JSchema schema, out IList<ValidationError> errors)
        {
            IList<ValidationError> schemaErrors = new List<ValidationError>();

            source.Validate(schema, (sender, args) => schemaErrors.Add(args.ValidationError));

            errors = schemaErrors;
            return (errors.Count == 0);
        }
        /// <summary>
        /// Determines whether the <see cref="JToken"/> is valid.
        /// </summary>
        /// <param name="source">The source <see cref="JToken"/> to test.</param>
        /// <param name="schema">The schema to test with.</param>
        /// <param name="errorMessages">When this method returns, contains any error messages generated while validating.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="JToken"/> is valid; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsValid(this JToken source, JSchema schema, out IList<string> errorMessages)
        {
            IList<string> errors = new List<string>();

            source.Validate(schema, (sender, args) => errors.Add(args.Message));

            errorMessages = errors;
            return (errorMessages.Count == 0);
        }
 public static void AssertSingleProperty(JSchema schema, string propertyName, JSchemaType schemaType, bool required = true)
 {
     Assert.IsNotNull(schema);
     Assert.IsNotNull(schema.Properties);
     Assert.IsTrue(schema.Properties.ContainsKey(propertyName), string.Format("property with name '{0}' expected.", propertyName));
     Assert.IsNotNull(schema.Properties[propertyName].Type);
     Assert.AreEqual(schemaType, schema.Properties[propertyName].Type.Value);
     if (required)
         Assert.IsTrue(schema.Required.Contains(propertyName), "Property {0} should be required", propertyName);
 }
		private CompilationUnitSyntax GetSyntax(JSchema schema)
		{
			var typeContext = new Dictionary<string, MemberDeclarationSyntax>();

			RoslynExtension.GenerateClass(schema, typeContext, string.Empty, _namingRule);

			var root = Syntax.CompilationUnit()
				.WithMembers(Syntax.NamespaceDeclaration(Syntax.ParseName(_namingRule.GetNamespaceName(schema))).WithMembers(Syntax.List(typeContext.Values.ToArray())));
			return root;
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="ExternalSchema"/> class with the specified URI and <see cref="JSchema"/>.
        /// </summary>
        /// <param name="uri">The schema URI.</param>
        /// <param name="schema">The schema.</param>
        public ExternalSchema(Uri uri, JSchema schema)
        {
            if (uri == null)
                throw new ArgumentNullException("uri");
            if (schema == null)
                throw new ArgumentNullException("schema");

            _uri = uri;
            _schema = schema;
        }
Exemple #14
0
        public string GetClassNameFromArrayItem(JSchema schema, string propertyName)
		{
			var className = GetPropertyName(schema.Title);
			if (string.IsNullOrEmpty(className))
			{
				className = Singular(propertyName);
			}

			return className;
		}
 /// <summary>
 /// Parameterized constructor
 /// </summary>
 /// <param name="schemaPath">Path of schema used for validation</param>
 public JsonExternalHandler(string schemaPath)
 {
     if (Path.GetFullPath(schemaPath) != null)
     {
         using (StreamReader file = File.OpenText(schemaPath))
         using (JsonTextReader reader = new JsonTextReader(file))
         {
             _schema = JSchema.Load(reader);
         }
     }
 }
Exemple #16
0
        public static void AssertSchemaIsValid(JSchema jSchema, JContainer jContainer)
        {
            IList<string> messages;
            var isValid = jContainer.IsValid(jSchema, out messages);
            foreach (var message in messages)
            {
                Console.WriteLine(message);
            }

            Assert.IsTrue(isValid);
        }
Exemple #17
0
        protected Type ArrayToType(string typeSignature, JSchema schema)
        {
            if(schema.Items.Count() == 1) {
                Type type = SchemaToType(typeSignature + "[]", schema.Items.First());
                return type.MakeArrayType();
            } else {
                // Handle tuples
            }

            return typeof(void);
        }
        private IEdmTypeReference MapArray(KeyValuePair<string, JSchema> property, JSchema parent, EdmModel model)
        {
            var entityPrimitiveType = ToEdmPrimitiveType(property.Value.Items.Single().Type.Value);

            var entityType = EdmCoreModel.Instance.GetPrimitiveType(entityPrimitiveType);

            var collectionType = new EdmCollectionType(new EdmPrimitiveTypeReference(entityType, false));

            bool isNullable = !parent.Required.Contains(property.Key);

            return new EdmCollectionTypeReference(collectionType, isNullable);
        }
        public IEdmModel ToEdmModel(JSchema schema)
        {
            var model = new EdmModel();

            var container = new EdmEntityContainer("namespace", "containerName");

            var rootType = ConvertToEdmEntityType(schema, model, "root");

            container.AddEntitySet("root", rootType);
            model.AddElement(container);

            return model;
        }
        private JSchemaValidatingReader CreateReader(string json, JSchema schema, out IList<SchemaValidationEventArgs> errors)
        {
            JsonReader reader = new JsonTextReader(new StringReader(json));

            List<SchemaValidationEventArgs> localErrors = new List<SchemaValidationEventArgs>();

            JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
            validatingReader.ValidationEventHandler += (sender, args) => { localErrors.Add(args); };
            validatingReader.Schema = schema;

            errors = localErrors;
            return validatingReader;
        }
Exemple #21
0
        public string GetClassName(JSchema schema, string propertyName)
		{
			var className = GetPropertyName(schema.Title);

			string namespaceName;
			className = ExtractClassName(className, out namespaceName);

			if (string.IsNullOrEmpty(className))
			{
				className = propertyName;
			}

			return className;
		}
        public void ValidateInteger()
        {
            JSchema schema = new JSchema();
            schema.Type = JSchemaType.Integer;

            string json = "1";

            IList<SchemaValidationEventArgs> errors;
            JSchemaValidatingReader validatingReader = CreateReader(json, schema, out errors);

            Assert.IsTrue(validatingReader.Read());

            Assert.AreEqual(0, errors.Count);
        }
		public async Task TestUseSchemaFromDefaultTemplateIfNoSkins()
		{
			var templateInfo = new StringTemplateInfo("test", "");
            var schema = new JSchema();

			var templateSchemaProvider = new Mock<ISchemaProvider>();
			templateSchemaProvider.Setup(f => f.GetSchemaFromTemplateAsync(templateInfo)).Returns(Task.FromResult(schema));

			var moduleDefintion = new ModuleDefinition("testmod", templateInfo, null);

			var underTest = new DefaultModuleSchemaProvider(templateSchemaProvider.Object);

			var result = await underTest.GetSchemaFromModuleAsync(moduleDefintion);
			Assert.AreEqual(schema, result);
		}
		public void TestTitleUsedFromAvailableSchema()
		{
			const string expectedResult = "title";

			var schema1 = new JSchema();
			var schema2 = new JSchema
			{
				Title = expectedResult
			};

			var underTest = new SchemaCombiner();
			var result = underTest.Apply(schema1, schema2, null);

			Assert.IsNotNull(result);
			Assert.AreEqual(expectedResult, result.Title);
		}
        /// <summary>
        /// Validates the specified <see cref="JToken"/>.
        /// </summary>
        /// <param name="source">The source <see cref="JToken"/> to test.</param>
        /// <param name="schema">The schema to test with.</param>
        /// <param name="validationEventHandler">The validation event handler.</param>
        public static void Validate(this JToken source, JSchema schema, SchemaValidationEventHandler validationEventHandler)
        {
            ValidationUtils.ArgumentNotNull(source, "source");
            ValidationUtils.ArgumentNotNull(schema, "schema");

            using (JSchemaValidatingReader reader = new JSchemaValidatingReader(source.CreateReader()))
            {
                reader.Schema = schema;
                if (validationEventHandler != null)
                    reader.ValidationEventHandler += validationEventHandler;

                while (reader.Read())
                {
                }
            }
        }
Exemple #26
0
        public JSchema Apply(JSchema schema, JSchema baseSchema, SchemaComparisionReport report, string propertyName = null)
        {
            if (schema == null)
                return baseSchema;

            if (baseSchema == null)
                return schema;

            if (!string.IsNullOrEmpty(baseSchema.Title))
                schema.Title = baseSchema.Title;

            if (!string.IsNullOrEmpty(baseSchema.Format))
                schema.Format = baseSchema.Format;

            if (baseSchema.Type != null)
            {
                if (!CanConvert(schema.Type, baseSchema.Type))
                    report.Push(new TypeChangeFailure(propertyName, schema, baseSchema));
                else
                    schema.Type = baseSchema.Type;
            }

            if (schema.Properties != null)
            {
                foreach (var property in schema.Properties)
                {
                    if (baseSchema.Properties == null || !baseSchema.Properties.ContainsKey(property.Key))
                    {
                        report.Push(new MissingPropertyInfo(property.Key, schema, baseSchema));
                        continue;
                    }

                    var baseProperty = baseSchema.Properties[property.Key];
                    Apply(property.Value, baseProperty, report, property.Key);
                }
            }

            if (schema.Items != null && baseSchema.Items != null)
            {
                for (int i = 0; i < schema.Items.Count && i < baseSchema.Items.Count; i++)
                {
                    Apply(schema.Items[i], baseSchema.Items[i], report);
                }
            }

            return schema;
        }
		public void TestCombineSchemaProperties()
		{
			var schema1 = new JSchema();
			schema1.Properties.Add("prop1", new JSchema());

			var schema2 = new JSchema();
			schema2.Properties.Add("prop2", new JSchema());

			var underTest = new SchemaCombiner();
			var result = underTest.Apply(schema1, schema2, null);

			Assert.NotNull(result);
			Assert.NotNull(result.Properties);
			Assert.Equal(2, result.Properties.Count);
			Assert.True(result.Properties.ContainsKey("prop1"), "Expect the property from the first schema.");
			Assert.True(result.Properties.ContainsKey("prop2"), "Expect the property from the second schema.");
		}
Exemple #28
0
        public MapStore(string directory)
        {
            _directory = Guard.NotNullOrEmpty(directory, "directory");

            _json = new JsonSerializer
            {
                MissingMemberHandling = MissingMemberHandling.Error
            };

            var stream = typeof(Program).Assembly.GetManifestResourceStream("DescentGlovePie.Generator.map.schema.json");
            if (stream == null)
                throw new ApplicationException("Missing assembly resource for map.schema.json.");

            using (stream)
            {
                _schema = JSchema.Load(new JsonTextReader(new StreamReader(stream)));
            }
        }
		public Type Compile(JSchema schema)
		{
			if (schema.Properties == null || schema.Properties.Count == 0)
				return typeof(object);

			var schemas = new[] { schema };
			using (var stream = new MemoryStream())
			{
				var result = CompileToInternal(schemas, stream);
				if (result.Success)
				{
					var typeName = _namingRule.GetClassName(schema, null);
					var assembly = Assembly.Load(stream.ToReadOnlyArray().ToArray());
					return assembly.GetTypes().First(t => t.Name == typeName);
				}
			}

			return null;
		}
        private void OnChanged(JSchema changedSchema)
        {
            if (State != JSchemaState.Default)
            {
                return;
            }

            try
            {
                KnownSchemas.Clear();

                State = JSchemaState.Reentrant;
                Changed?.Invoke(changedSchema);
            }
            finally
            {
                State = JSchemaState.Default;
            }
        }
        internal static ValidationError CreateValidationError(IFormattable message, ErrorType errorType, JSchema schema, Uri schemaId, object value, IList <ValidationError> childErrors, IJsonLineInfo lineInfo, string path)
        {
            ValidationError error = new ValidationError();

            error._formattable = message;
            error._childErrors = childErrors;

            error.ErrorType = errorType;
            error.Path      = path;
            if (lineInfo != null)
            {
                error.LineNumber   = lineInfo.LineNumber;
                error.LinePosition = lineInfo.LinePosition;
            }
            error.Schema        = schema;
            error.SchemaId      = schemaId;
            error.SchemaBaseUri = schema.BaseUri;
            error.Value         = value;
            return(error);
        }
        private JSchema GenerateInternal(Type type, Required valueRequired, JsonProperty memberProperty, JsonContainerContract container)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            Type nonNullableType = ReflectionUtils.IsNullableType(type) ? Nullable.GetUnderlyingType(type) : type;

            Uri explicitId = GetTypeId(nonNullableType, true);

            JsonContract contract = ContractResolver.ResolveContract(type);

            var key = CreateKey(valueRequired, memberProperty, contract);

            switch (contract.ContractType)
            {
            case JsonContractType.Object:
            case JsonContractType.Array:
            case JsonContractType.Dictionary:
                TypeSchema typeSchema = _typeSchemas.SingleOrDefault(s => s.Key.Equals(key));
                if (typeSchema != null)
                {
                    return(typeSchema.Schema);
                }
                break;
            }

            JSchema schema = null;

            JSchemaGenerationProvider provider = ResolveTypeProvider(nonNullableType, memberProperty);

            if (provider != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, valueRequired, memberProperty, container, this);

                schema = provider.GetSchema(context);

                if (schema == null)
                {
                    throw new JSchemaException("Could not get schema for type '{0}' from provider '{1}'.".FormatWith(CultureInfo.InvariantCulture, type.FullName, provider.GetType().FullName));
                }
            }

            if (_generationProviders != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, valueRequired, memberProperty, container, this);

                foreach (JSchemaGenerationProvider generationProvider in _generationProviders)
                {
                    schema = generationProvider.GetSchema(context);
                }
            }

            if (schema != null)
            {
                _typeSchemas.Add(new TypeSchema(key, schema));
                return(schema);
            }

            schema = new JSchema();
            if (explicitId != null)
            {
                schema.Id = explicitId;
            }

            switch (contract.ContractType)
            {
            case JsonContractType.Object:
            case JsonContractType.Array:
            case JsonContractType.Dictionary:
                _typeSchemas.Add(new TypeSchema(key, schema));
                break;
            }

            return(PopulateSchema(schema, contract, memberProperty, valueRequired));
        }
        private JSchema PopulateSchema(JSchema schema, JsonContract contract, JsonProperty memberProperty, Required valueRequired)
        {
            schema.Title       = GetTitle(contract.NonNullableUnderlyingType);
            schema.Description = GetDescription(contract.NonNullableUnderlyingType);

            JsonConverter converter = contract.Converter ?? contract.InternalConverter;

            if (converter != null)
            {
                schema.Type = null;
            }
            else
            {
                switch (contract.ContractType)
                {
                case JsonContractType.Object:
                    if (schema.Id == null)
                    {
                        schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false);
                    }

                    schema.Type = AddNullType(JSchemaType.Object, valueRequired);
                    GenerateObjectSchema(schema, contract.NonNullableUnderlyingType, (JsonObjectContract)contract);
                    break;

                case JsonContractType.Array:
                    if (schema.Id == null)
                    {
                        schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false);
                    }

                    schema.Type         = AddNullType(JSchemaType.Array, valueRequired);
                    schema.MinimumItems = DataAnnotationHelpers.GetMinLength(memberProperty);
                    schema.MaximumItems = DataAnnotationHelpers.GetMaxLength(memberProperty);

                    JsonArrayAttribute arrayAttribute = JsonTypeReflector.GetCachedAttribute <JsonArrayAttribute>(contract.NonNullableUnderlyingType);
                    bool allowNullItem = (arrayAttribute == null || arrayAttribute.AllowNullItems);

                    Type collectionItemType = ReflectionUtils.GetCollectionItemType(contract.NonNullableUnderlyingType);
                    if (collectionItemType != null)
                    {
                        schema.Items.Add(GenerateInternal(collectionItemType, (!allowNullItem) ? Required.Always : Required.Default, null, (JsonArrayContract)contract));
                    }
                    break;

                case JsonContractType.Primitive:
                    schema.Type = GetJSchemaType(contract.UnderlyingType, valueRequired);

                    if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String))
                    {
                        int minimumLength;
                        int maximumLength;
                        if (DataAnnotationHelpers.GetStringLength(memberProperty, out minimumLength, out maximumLength))
                        {
                            schema.MinimumLength = minimumLength;
                            schema.MaximumLength = maximumLength;
                        }
                        else
                        {
                            schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty);
                            schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty);
                        }

                        schema.Pattern = DataAnnotationHelpers.GetPattern(memberProperty);
                        schema.Format  = DataAnnotationHelpers.GetFormat(memberProperty);
                    }
                    if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Number) || JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Integer))
                    {
                        double minimum;
                        double maximum;
                        if (DataAnnotationHelpers.GetRange(memberProperty, out minimum, out maximum))
                        {
                            schema.Minimum = minimum;
                            schema.Maximum = maximum;
                        }
                    }

                    if (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.Integer) &&
                        contract.NonNullableUnderlyingType.IsEnum() &&
                        ReflectionUtils.GetAttribute <FlagsAttribute>(contract.NonNullableUnderlyingType) == null)
                    {
                        IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(contract.NonNullableUnderlyingType);
                        foreach (EnumValue <long> enumValue in enumValues)
                        {
                            JToken value = JToken.FromObject(enumValue.Value);

                            schema.Enum.Add(value);
                        }
                    }

                    Type enumDataType = DataAnnotationHelpers.GetEnumDataType(memberProperty);
                    if (enumDataType != null && CollectionUtils.IsNullOrEmpty(schema._enum))
                    {
                        IList <EnumValue <long> > enumValues = EnumUtils.GetNamesAndValues <long>(enumDataType);
                        foreach (EnumValue <long> enumValue in enumValues)
                        {
                            JToken value = (JSchemaTypeHelpers.HasFlag(schema.Type, JSchemaType.String))
                                    ? enumValue.Name
                                    : JToken.FromObject(enumValue.Value);

                            schema.Enum.Add(value);
                        }
                    }
                    break;

                case JsonContractType.String:
                    JSchemaType schemaType = (!ReflectionUtils.IsNullable(contract.UnderlyingType))
                            ? JSchemaType.String
                            : AddNullType(JSchemaType.String, valueRequired);

                    schema.Type          = schemaType;
                    schema.MinimumLength = DataAnnotationHelpers.GetMinLength(memberProperty);
                    schema.MaximumLength = DataAnnotationHelpers.GetMaxLength(memberProperty);
                    break;

                case JsonContractType.Dictionary:
                    schema.Type = AddNullType(JSchemaType.Object, valueRequired);
                    schema.MinimumProperties = DataAnnotationHelpers.GetMinLength(memberProperty);
                    schema.MaximumProperties = DataAnnotationHelpers.GetMaxLength(memberProperty);

                    Type keyType;
                    Type valueType;
                    ReflectionUtils.GetDictionaryKeyValueTypes(contract.NonNullableUnderlyingType, out keyType, out valueType);

                    if (keyType != null)
                    {
                        JsonContract keyContract = ContractResolver.ResolveContract(keyType);

                        // can be converted to a string
                        if (keyContract.ContractType == JsonContractType.Primitive)
                        {
                            schema.AdditionalProperties = GenerateInternal(valueType, Required.Default, null, (JsonDictionaryContract)contract);
                        }
                    }
                    break;

                case JsonContractType.Serializable:
                    if (schema.Id == null)
                    {
                        schema.Id = GetTypeId(contract.NonNullableUnderlyingType, false);
                    }

                    schema.Type = AddNullType(JSchemaType.Object, valueRequired);
                    schema.AllowAdditionalProperties = true;
                    break;

                case JsonContractType.Dynamic:
                case JsonContractType.Linq:
                    schema.Type = null;
                    break;

                default:
                    throw new JSchemaException("Unexpected contract type: {0}".FormatWith(CultureInfo.InvariantCulture, contract));
                }
            }

            return(schema);
        }
Exemple #34
0
        /// <summary>
        /// Adds a <see cref="JSchema"/> to the <see cref="JSchemaPreloadedResolver"/> store and maps it to a URI taken from the root schema's ID.
        /// If the store already contains a mapping for the same URI, the existing mapping is overridden.
        /// </summary>
        /// <param name="schema">The schema that is being added to the <see cref="JSchemaPreloadedResolver"/> store.</param>
        public void Add(JSchema schema)
        {
            Uri resolvedId = schema.Id ?? new Uri(string.Empty, UriKind.RelativeOrAbsolute);

            Add(schema, resolvedId);
        }
 public override ValidationError CreateError(string message, ErrorType errorType, JSchema schema, object value, IList <ValidationError> childErrors)
 {
     return(CreateError(message, errorType, schema, value, childErrors, null, _writer.Path));
 }
 /// <summary>
 /// Determines whether this instance should validate with the specified <see cref="JSchema"/>.
 /// </summary>
 /// <param name="schema">The <see cref="JSchema"/>.</param>
 /// <returns>
 ///     <c>true</c> if this instance should validate with the specified <see cref="JSchema"/>; otherwise, <c>false</c>.
 /// </returns>
 public abstract bool CanValidate(JSchema schema);
 /// <summary>
 /// Validates the specified <see cref="JToken"/>.
 /// </summary>
 /// <param name="source">The source <see cref="JToken"/> to test.</param>
 /// <param name="schema">The schema to test with.</param>
 public static void Validate(this JToken source, JSchema schema)
 {
     source.Validate(schema, null);
 }
Exemple #38
0
 public TypeSchema(TypeSchemaKey key, JSchema schema)
 {
     Key    = key;
     Schema = schema;
 }
Exemple #39
0
 public override ValidationError CreateError(IFormattable message, ErrorType errorType, JSchema schema, object value, IList <ValidationError> childErrors)
 {
     return(CreateError(message, errorType, schema, value, childErrors, _reader, _reader.Path));
 }
Exemple #40
0
        public virtual string AddData(string data, string id, string createdBy, bool validateSchema = true, bool skipOCR = false)
        {
            Newtonsoft.Json.Linq.JObject obj = Newtonsoft.Json.Linq.JObject.Parse(data);
            dynamic objDyn = Newtonsoft.Json.Linq.JObject.Parse(data);

            if (validateSchema)
            {
                Newtonsoft.Json.Schema.JSchema schema = DataSetDB.Instance.GetRegistration(this.datasetId).GetSchema();

                if (schema != null)
                {
                    IList <string> errors;
                    if (!obj.IsValid(schema, out errors))
                    {
                        if (errors == null || errors?.Count == 0)
                        {
                            errors = new string[] { "", "" }
                        }
                        ;

                        throw DataSetException.GetExc(this.datasetId,
                                                      ApiResponseStatus.DatasetItemInvalidFormat.error.number,
                                                      ApiResponseStatus.DatasetItemInvalidFormat.error.description,
                                                      errors.Aggregate((f, s) => f + ";" + s)
                                                      );
                    }
                }
            }
            if (string.IsNullOrEmpty(id))
            {
                throw new DataSetException(this.datasetId, ApiResponseStatus.DatasetItemNoSetID);
            }

            if (objDyn.Id == null
                &&
                objDyn.id == null)
            {
                throw new DataSetException(this.datasetId, ApiResponseStatus.DatasetItemNoSetID);
            }
            else
            {
                id = objDyn.Id == null ? (string)objDyn.id : (string)objDyn.Id;
            }

            objDyn.DbCreated   = DateTime.UtcNow;
            objDyn.DbCreatedBy = createdBy;



            //check special HsProcessType
            var jobj   = (Newtonsoft.Json.Linq.JObject)objDyn;
            var jpaths = jobj
                         .SelectTokens("$..HsProcessType")
                         .ToArray();
            var jpathObjs = jpaths.Select(j => j.Parent.Parent).ToArray();

            if (this.DatasetId == DataSetDB.DataSourcesDbName) //don't analyze for registration of new dataset
            {
                jpathObjs = new JContainer[] { }
            }
            ;

            foreach (var jo in jpathObjs)
            {
                if (jo["HsProcessType"].Value <string>() == "person")
                {
                    var jmenoAttrName = jo.Children()
                                        .Select(c => c as JProperty)
                                        .Where(c => c != null)
                                        .Where(c => c.Name.ToLower() == "jmeno" ||
                                               c.Name.ToLower() == "name")
                                        .FirstOrDefault()?.Name;
                    var prijmeniAttrName = jo.Children()
                                           .Select(c => c as JProperty)
                                           .Where(c => c != null)
                                           .Where(c => c.Name.ToLower() == "prijmeni" ||
                                                  c.Name.ToLower() == "surname")
                                           .FirstOrDefault()?.Name;
                    var narozeniAttrName = jo.Children()
                                           .Select(c => c as JProperty)
                                           .Where(c => c != null)
                                           .Where(c => c.Name.ToLower() == "narozeni" ||
                                                  c.Name.ToLower() == "birthdate")
                                           .FirstOrDefault()?.Name;
                    var osobaIdAttrName = jo.Children()
                                          .Select(c => c as JProperty)
                                          .Where(c => c != null)
                                          .Where(c => c.Name.ToLower() == "osobaid")
                                          .FirstOrDefault()?.Name ?? "OsobaId";

                    var celejmenoAttrName = jo.Children()
                                            .Select(c => c as JProperty)
                                            .Where(c => c != null)
                                            .Where(c => c.Name.ToLower() == "celejmeno" ||
                                                   c.Name.ToLower() == "fullname")
                                            .FirstOrDefault()?.Name;


                    #region FindOsobaId
                    if (jmenoAttrName != null && prijmeniAttrName != null && narozeniAttrName != null)
                    {
                        if (string.IsNullOrEmpty(jo["OsobaId"]?.Value <string>()) &&
                            jo[narozeniAttrName] != null && jo[narozeniAttrName].Value <DateTime?>().HasValue
                            ) //pokud OsobaId je vyplnena, nehledej jinou
                        {
                            string osobaId   = null;
                            var    osobaInDb = Osoba.GetByName(
                                jo[jmenoAttrName].Value <string>(),
                                jo[prijmeniAttrName].Value <string>(),
                                jo[narozeniAttrName].Value <DateTime>()
                                );
                            if (osobaInDb == null)
                            {
                                osobaInDb = Osoba.GetByNameAscii(
                                    jo[jmenoAttrName].Value <string>(),
                                    jo[prijmeniAttrName].Value <string>(),
                                    jo[narozeniAttrName].Value <DateTime>()
                                    );
                            }

                            if (osobaInDb != null && string.IsNullOrEmpty(osobaInDb.NameId))
                            {
                                osobaInDb.NameId = osobaInDb.GetUniqueNamedId();
                                osobaInDb.Save();
                            }
                            osobaId       = osobaInDb?.NameId;
                            jo["OsobaId"] = osobaId;
                        }
                    }
                    else if (celejmenoAttrName != null && narozeniAttrName != null)
                    {
                        if (string.IsNullOrEmpty(jo["OsobaId"]?.Value <string>()) &&
                            jo[narozeniAttrName].Value <DateTime?>().HasValue
                            ) //pokud OsobaId je vyplnena, nehledej jinou
                        {
                            string         osobaId      = null;
                            Lib.Data.Osoba osobaZeJmena = Lib.Validators.OsobaInText(jo[celejmenoAttrName].Value <string>());
                            if (osobaZeJmena != null)
                            {
                                var osobaInDb = Osoba.GetByName(
                                    osobaZeJmena.Jmeno,
                                    osobaZeJmena.Prijmeni,
                                    jo[narozeniAttrName].Value <DateTime>()
                                    );

                                if (osobaInDb == null)
                                {
                                    osobaInDb = Osoba.GetByNameAscii(
                                        osobaZeJmena.Jmeno,
                                        osobaZeJmena.Prijmeni,
                                        jo[narozeniAttrName].Value <DateTime>()
                                        );
                                }

                                if (osobaInDb != null && string.IsNullOrEmpty(osobaInDb.NameId))
                                {
                                    osobaInDb.NameId = osobaInDb.GetUniqueNamedId();
                                    osobaInDb.Save();
                                }
                                osobaId = osobaInDb?.NameId;
                            }
                            jo["OsobaId"] = osobaId;
                        }
                    }

                    #endregion
                }
            }


            string   updatedData = Newtonsoft.Json.JsonConvert.SerializeObject(objDyn);
            PostData pd          = PostData.String(updatedData);

            var tres = client.LowLevel.Index <StringResponse>(client.ConnectionSettings.DefaultIndex, id, pd); //todo: es7 check

            if (tres.Success)
            {
                Newtonsoft.Json.Linq.JObject jobject = Newtonsoft.Json.Linq.JObject.Parse(tres.Body);

                string finalId = jobject.Value <string>("_id");

                //do DocumentMining after successfull save
                //record must exists before document mining
                bool needsOCR = false;
                if (skipOCR == false)
                {
                    foreach (var jo in jpathObjs)
                    {
                        if (jo["HsProcessType"].Value <string>() == "document")
                        {
                            if (jo["DocumentUrl"] != null && string.IsNullOrEmpty(jo["DocumentPlainText"].Value <string>()))
                            {
                                if (Uri.TryCreate(jo["DocumentUrl"].Value <string>(), UriKind.Absolute, out var uri2Ocr))
                                {
                                    //get text from document
                                    //var url = Devmasters.Core.Util.Config.GetConfigValue("ESConnection");
                                    //url = url + $"/{client.ConnectionSettings.DefaultIndex}/data/{finalId}/_update";
                                    //string callback = HlidacStatu.Lib.OCR.Api.CallbackData.PrepareElasticCallbackDataForOCRReq($"{jo.Path}.DocumentPlainText", false);
                                    //var ocrCallBack = new HlidacStatu.Lib.OCR.Api.CallbackData(new Uri(url), callback, HlidacStatu.Lib.OCR.Api.CallbackData.CallbackType.LocalElastic);
                                    //HlidacStatu.Lib.OCR.Api.Client.TextFromUrl(
                                    //    Devmasters.Core.Util.Config.GetConfigValue("OCRServerApiKey"),
                                    //    uri2Ocr, "Dataset+" + createdBy,
                                    //    HlidacStatu.Lib.OCR.Api.Client.TaskPriority.Standard, HlidacStatu.Lib.OCR.Api.Client.MiningIntensity.Maximum
                                    //    ); //TODOcallBackData: ocrCallBack);

                                    needsOCR = true;
                                }
                            }
                        }
                    }
                }
                if (needsOCR)
                {
                    Lib.Data.ItemToOcrQueue.AddNewTask(ItemToOcrQueue.ItemToOcrType.Dataset, finalId, this.datasetId, OCR.Api.Client.TaskPriority.Standard);
                }

                return(finalId);
            }
            else
            {
                var status = ApiResponseStatus.DatasetItemSaveError;
                if (tres.TryGetServerError(out var servererr))
                {
                    status.error.errorDetail = servererr.Error.ToString();
                }
                throw new DataSetException(this.datasetId, status);
            }

            //return res.ToString();
            //ElasticsearchResponse<string> result = this.client.Raw.Index(document.Index, document.Type, document.Id, documentJson);
        }
 internal JsonValidatorContext(Scope scope, JSchema schema)
 {
     _scope  = scope;
     _schema = schema;
 }
 internal void OnChildChanged(JSchema changedSchema)
 {
     OnChanged(changedSchema);
 }