示例#1
0
        public IFileValueConnectors GetConnectors(string jsonConfig)
        {
            if (string.IsNullOrWhiteSpace(jsonConfig))
            {
                return(new NoFileValueConnectors());
            }
            try
            {
                var validator = new NJsonSchema.Validation.JsonSchemaValidator().Validate(jsonConfig, this.GetConnectorsSchema());
                if (validator.Where(v => !(new[] { NJsonSchema.Validation.ValidationErrorKind.NoAdditionalPropertiesAllowed, NJsonSchema.Validation.ValidationErrorKind.AdditionalPropertiesNotValid }.Contains(v.Kind) && v.Path == "#/$schema")).Count() > 0)
                {
                    return(new NoFileValueConnectors());
                }
            }
            catch
            {
                return(new NoFileValueConnectors());
            }
            Dictionary <string, IProviderProcessorAdapter> adapterDictionary = _providerProcessorAdapter.ToDictionary(i => i.Name);
            JObject o    = JObject.Parse(jsonConfig);
            var     elts = o.Properties()
                           .Where(p => p.Path != "$schema")
                           .Select(i => ParseConnections(i, adapterDictionary));
            var processors = elts.SelectMany(i => i.processors).ToDictionary(i => i.Code);
            var providers  = elts.SelectMany(i => i.providers).ToDictionary(i => i.Code);

            return(new FileValueConnectors(providers, processors));
        }
        public static int ValidateJSONAlternative(AasValidationRecordList recs, Stream jsonContent)
        {
            // see: https://github.com/RicoSuter/NJsonSchema/wiki/JsonSchemaValidator
            var newRecs = new AasValidationRecordList();

            // access
            if (recs == null || jsonContent == null)
            {
                return(-1);
            }

            // Load the schema files
            // right now: exactly ONE schema file
            var files = GetSchemaResources(SerializationFormat.JSON);

            if (files == null || files.Length != 1)
            {
                return(-1);
            }

            NJsonSchema.JsonSchema schema = null;

            try
            {
                Assembly myAssembly = Assembly.GetExecutingAssembly();
                foreach (var schemaFn in files)
                {
                    using (Stream schemaStream = myAssembly.GetManifestResourceStream(schemaFn))
                    {
                        using (var streamReader = new StreamReader(schemaStream))
                        {
                            var allTxt = streamReader.ReadToEnd();
                            schema = NJsonSchema.JsonSchema.FromJsonAsync(allTxt).GetAwaiter().GetResult();
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FileNotFoundException("ValidateJSON: Error loading schema: " +
                                                ex.Message);
            }

            if (schema == null)
            {
                throw new FileNotFoundException("ValidateJSON: Schema not found properly.");
            }

            // create validator
            var validator = new NJsonSchema.Validation.JsonSchemaValidator();

            // load the JSON content
            string jsonTxt = null;

            try
            {
                using (var streamReader = new StreamReader(jsonContent))
                {
                    jsonTxt = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("ValidateJSON: Error loading JSON content: " +
                                                    ex.Message);
            }

            if (jsonTxt == null || jsonTxt == "")
            {
                throw new InvalidOperationException("ValidateJSON: Error loading JSON content gave null.");
            }

            // validate
            ICollection <NJsonSchema.Validation.ValidationError> errors;

            try
            {
                errors = validator.Validate(jsonTxt, schema);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("ValidateJSON: Error when validating: " +
                                                    ex.Message);
            }

            // re-format messages
            if (errors != null)
            {
                foreach (var ve in errors)
                {
                    var msg = ("" + ve.ToString());
                    msg = Regex.Replace(msg, @"\s+", " ");
                    newRecs.Add(new AasValidationRecord(AasValidationSeverity.Serialization, null,
                                                        $"JSON: {ve.LineNumber,5},{ve.LinePosition:3}: {msg}"));
                }
            }

            // result
            recs.AddRange(newRecs);
            return(newRecs.Count);
        }