示例#1
0
        /// <summary>
        /// Validates a <see cref="Device"/>.
        /// </summary>
        /// <param name="device">Device to validate.</param>
        /// <returns>Validation errors.</returns>
        public static IEnumerable <string> Validate(Device device)
        {
            var validationErrors = new List <string>();

            if (string.IsNullOrEmpty(device.Id))
            {
                validationErrors.Add($"Device Id is missing for Device of type {device.Type}");
                return(validationErrors);
            }

            if (device.Type == DeviceType.Unknown)
            {
                validationErrors.Add($"Device Type is missing or not a valid type on Device id {device.Id}");
                return(validationErrors);
            }

            validationErrors.AddRange(DeviceInfoValidator.Validate(device.DeviceInfo));
            validationErrors.AddRange(NameInfoValidator.Validate(device.Name));
            validationErrors.AddRange(CustomDataValidator.Validate(device.CustomData));

            if (device.Traits != null)
            {
                foreach (var trait in device.Traits)
                {
                    validationErrors.AddRange(DeviceTraitValidator.Validate(trait).Select(x => $"{x} on Device id {device.Id}"));
                }
            }

            return(validationErrors);
        }
        /// <summary>
        /// Validates a <see cref="Device"/>.
        /// </summary>
        /// <param name="device">Device to validate.</param>
        /// <returns>Validation errors.</returns>
        public static IEnumerable <string> Validate(Device device)
        {
            var validationErrors = new List <string>();

            if (string.IsNullOrEmpty(device.Id))
            {
                validationErrors.Add("Device Id is missing");
            }

            if (device.Type == Models.DeviceType.Unknown)
            {
                validationErrors.Add("Device Type is missing or not a valid type");
            }

            validationErrors.AddRange(DeviceInfoValidator.Validate(device.DeviceInfo));
            validationErrors.AddRange(NameInfoValidator.Validate(device.Name));
            validationErrors.AddRange(CustomDataValidator.Validate(device.CustomData));

            if (device.Traits != null)
            {
                foreach (var trait in device.Traits)
                {
                    validationErrors.AddRange(DeviceTraitValidator.Validate(trait));
                }
            }

            return(validationErrors);
        }
        /// <summary>
        /// Validates a <see cref="Device"/>.
        /// </summary>
        /// <param name="device">Device to validate.</param>
        /// <returns>Validation errors.</returns>
        public static IEnumerable <string> Validate(Device device)
        {
            var validationErrors = new List <string>();

            if (string.IsNullOrEmpty(device.Id))
            {
                validationErrors.Add($"Device Id is missing for Device of type {device.Type}");
                return(validationErrors);
            }

            if (device.Type == DeviceType.Unknown)
            {
                validationErrors.Add($"Device Type is missing or not a valid type on Device id {device.Id}");
                return(validationErrors);
            }

            validationErrors.AddRange(DeviceInfoValidator.Validate(device.DeviceInfo));
            validationErrors.AddRange(NameInfoValidator.Validate(device.Name));
            validationErrors.AddRange(CustomDataValidator.Validate(device.CustomData));

            if (device.Traits != null)
            {
                foreach (var trait in device.Traits)
                {
                    if (DeviceMetadata.SupportedTraits.TryGetValue(device.Type, out IList <TraitType> supportedTraits))
                    {
                        if (supportedTraits.Contains(trait.Trait))
                        {
                            validationErrors.AddRange(DeviceTraitValidator.Validate(trait).Select(x => $"{x} on Device id {device.Id}"));
                        }
                        else
                        {
                            validationErrors.Add($"Trait {trait.Trait} not supported for Device type {device.Type} on Device id {device.Id}");
                        }
                    }
                }
            }

            return(validationErrors);
        }