Exemple #1
0
        //public List<TaskPropertyValidation> ValidPropertyList { get; set; } = new List<TaskPropertyValidation>();
        //public List<TaskPropertyValidation> InValidPropertyList { get; set; } = new List<TaskPropertyValidation>();

        // public TaskPropertyDictionary PropertyDictionary { get; set; } = new TaskPropertyDictionary();

        public void Publish(folderNode folder, aceAuthorNotation notation)
        {
            objectSerialization.saveObjectToXML(task, folder.pathFor(task.name + "_declaration.xml", imbSCI.Data.enums.getWritableFileMode.overwrite, "Declaration of task [" + task.name + "]"));

            ReportTable_PropertyValudation = PropertyValidation.GetReportTable(this);
            var pvr = PropertyValidation.GetResults();

            foreach (var pv in pvr[ValidationOutcome.Invalid])
            {
                reporter.AppendLine(pv.item.PropertyName + " : " + pv.item.DisplayName);
                reporter.AppendLine(pv.Outcome + " : " + pv.Message);
                reporter.nextTabLevel();

                reporter.AppendParagraph(pv.reporter.GetContent());
                reporter.prevTabLevel();
            }
            reporter.ReportSave(folder, task.name + "_validation_log.txt");


            task.ExtractorCustomizationSettings.ReportSave(folder, task.name + "_settings", "Custom settings for extractor [" + task.ExtractorName + "] stored in task [" + task.name + "]");

            //foreach (var t in ReportTables)
            //{
            //    t.GetReportAndSave(folder, notation, task.name);
            //}
        }
            /// <summary>
            /// Writes the JSON representation of the object.
            /// </summary>
            /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
            /// <param name="value">The value.</param>
            /// <param name="serializer">The calling serializer.</param>
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                Type valueType = value.GetType();

                // We don't want to use the type name of the enum itself; instead we marshal
                // it as a decimal number inside of a string
                if (valueType.GetTypeInfo().IsEnum)
                {
                    value     = ((Enum)value).ToString("D");
                    valueType = typeof(String);
                }

                PropertyValidation.ValidatePropertyValue(WebApiResources.SerializingPhrase(), value);

                //write out as an object with type information
                writer.WriteStartObject();
                writer.WritePropertyName(TypePropertyName);

                // Check that the Type we're claiming is safely deserializable
                String typeName = valueType.FullName;

                if (!PropertyValidation.IsValidTypeString(typeName))
                {
                    throw new PropertyTypeNotSupportedException(TypePropertyName, valueType);
                }

                writer.WriteValue(typeName);
                writer.WritePropertyName(ValuePropertyName);
                writer.WriteValue(value);
                writer.WriteEndObject();
            }
 internal PropertiesCollection(IDictionary <String, Object> source, bool validateExisting)
 {
     if (validateExisting)
     {
         PropertyValidation.ValidateDictionary(source);
     }
     m_innerDictionary      = new Dictionary <String, Object>(source, VssStringComparer.PropertyName);
     this.ValidateNewValues = true;
 }
        /// <summary>
        /// Implements IDictionary&lt;String, Object&gt;.Add
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(String key, Object value)
        {
            if (this.ValidateNewValues)
            {
                PropertyValidation.ValidatePropertyName(key);
                PropertyValidation.ValidatePropertyValue(key, value);
            }

            m_innerDictionary.Add(key, value);
        }
        void ICollection <KeyValuePair <String, Object> > .Add(KeyValuePair <String, Object> keyValuePair)
        {
            if (this.ValidateNewValues)
            {
                PropertyValidation.ValidatePropertyName(keyValuePair.Key);
                PropertyValidation.ValidatePropertyValue(keyValuePair.Key, keyValuePair.Value);
            }

            ((ICollection <KeyValuePair <String, Object> >)m_innerDictionary).Add(keyValuePair);
        }
Exemple #6
0
        public Double GetScore()
        {
            ScoreFactor_MetaTables             = ValidMetaTables.GetRatio(ValidationRunCount, 0, 0);
            ScoreFactor_PropertyCountRange     = 1.GetRatio(PropertyCount.Range);
            ScoreFactor_PropertyValidationRate = Math.Pow(PropertyValidation.GetScore(), 3);
            ScoreFactor_PropertyNameDiversity  = Math.Pow(PropertyValidation.GetPropertyNameDiversityScore(), 3);

            Double output = ScoreFactor_MetaTables * ScoreFactor_PropertyCountRange * ScoreFactor_PropertyValidationRate * ScoreFactor_PropertyNameDiversity;

            Score = output;

            return(output);
        }
        public void constraints_are_executed_in_order_of_registration()
        {
            int[] expected = { 2, 6 };
            var obj = new PropertyValidation();
            obj.Validate("A");

            Expect(obj.ExecutionList.Count, Is.EqualTo(expected.Length));

            for (int i = 0; i < expected.Length; i++)
            {
                Expect(obj.ExecutionList[i], Is.EqualTo(expected[i]));
            }
        }
        /// <summary>
        /// Implements IDictionary&lt;String, Object&gt;.Item
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Object this[String key]
        {
            get
            {
                return(m_innerDictionary[key]);
            }
            set
            {
                if (this.ValidateNewValues)
                {
                    PropertyValidation.ValidatePropertyName(key);
                    PropertyValidation.ValidatePropertyValue(key, value);
                }

                m_innerDictionary[key] = value;
            }
        }
Exemple #9
0
        public RegisterResult RegisterRequestValid(RegisterRequestDto registerRequestDto)
        {
            if (registerRequestDto == null)
            {
                return(RegisterResult.Unknown);
            }
            if (
                !PropertyValidation.IsValidEmail(registerRequestDto.Email) ||
                !PropertyValidation.IsValidUsername(registerRequestDto.Username) ||
                !PropertyValidation.IsValidName(registerRequestDto.Firstname) ||
                !PropertyValidation.IsValidName(registerRequestDto.Lastname) ||
                !PropertyValidation.IsValidPassword(registerRequestDto.Password) ||
                !PropertyValidation.IsValidPassword(registerRequestDto.Password2)
                )
            {
                return(RegisterResult.MissingInformation);
            }

            if (registerRequestDto.Password != registerRequestDto.Password2)
            {
                return(RegisterResult.PasswordMismatch);
            }

            bool usernameTaken = _userRepository.CheckIfUsernameExists(registerRequestDto.Username);

            if (usernameTaken)
            {
                return(RegisterResult.UsernameTaken);
            }

            bool emailTaken = _userRepository.CheckIfEmailExists(registerRequestDto.Email);

            if (emailTaken)
            {
                return(RegisterResult.EmailTaken);
            }

            return(RegisterResult.Success);
        }
        public bool ResetPassword(ResetPasswordDto resetPasswordDto)
        {
            if (resetPasswordDto == null)
            {
                return(false);
            }

            var isValidPw = PropertyValidation.IsValidPassword(resetPasswordDto.NewPassword);

            if (!isValidPw)
            {
                return(false);
            }

            var res = _userRepository.TryUseConfirmationLink(resetPasswordDto.Token, ConfirmType.EmailConfirmation);

            if (res.Success != ConfirmKeyUsageSuccess.Success)
            {
                return(false);
            }
            return(_userRepository.UpdatePassword(resetPasswordDto.Token, PasswordHasher.Hash(resetPasswordDto.NewPassword)));
        }
Exemple #11
0
        public static IEnumerable <ValidationErrorDetail> GetValidationResults <TObject>(this PropertyValidation validation, TObject itemToValidate, string displayName = null)
        {
            displayName = displayName ?? itemToValidate?.GetType().Name; // Set displayName to Type of TObject if not provided.
            var results = new Lazy <List <ValidationErrorDetail> >();

            foreach (var rule in validation.ValidationRules)
            {
                try
                {
                    rule.Validate(
                        validation.CallGetter(itemToValidate),       // Calls the Property getter on the Model
                        $"{displayName}.{validation.PropertyName}"); // Name that will be displayed in the error message
                }
                catch (ValidationException e)
                {
                    results.Value.Add(new ValidationErrorDetail {
                        ValidationResult = e.ValidationResult,
                        PropertyName     = validation.PropertyName,
                        Value            = e.Value,
                        Message          = e.Message
                    });
                    continue;
                }
            }

            return(results.IsValueCreated // If no results, don't instantiate
                ? results.Value
                : new List <ValidationErrorDetail>());
        }
        /// <summary>
        /// Gets the value of T associated with the specified key if the value can be converted to T according to <see cref="PropertyValidation"/>.
        /// </summary>
        /// <typeparam name="T">the type of the value associated with the specified key</typeparam>
        /// <param name="dictionary">the dictionary from which we should retrieve the value</param>
        /// <param name="key">the key of the value to retrieve</param>
        /// <param name="value">when this method returns, the value associated with the specified key, if the key is found and the value is convertible to T,
        /// or default of T, if not</param>
        /// <returns>true if the value was retrieved successfully, otherwise false</returns>
        public static bool TryGetValidatedValue <T>(this IDictionary <string, object> dictionary, string key, out T value, bool allowNull = true)
        {
            value = default(T);
            //try to convert to T. T *must* be something with
            //TypeCode != TypeCode.object (and not DBNull) OR
            //byte[] or guid or object.
            if (!PropertyValidation.IsValidConvertibleType(typeof(T)))
            {
                return(false);
            }

            //special case guid...
            if (typeof(T) == typeof(Guid))
            {
                Guid guidVal;
                if (dictionary.TryGetGuid(key, out guidVal))
                {
                    value = (T)(object)guidVal;
                    return(true);
                }
            }
            else
            {
                object objValue = null;
                if (dictionary.TryGetValue(key, out objValue))
                {
                    if (objValue == null)
                    {
                        //we found it and it is
                        //null, which may be okay depending on the allowNull flag
                        //value is already = default(T)
                        return(allowNull);
                    }

                    if (typeof(T).GetTypeInfo().IsAssignableFrom(objValue.GetType().GetTypeInfo()))
                    {
                        value = (T)objValue;
                        return(true);
                    }

                    if (typeof(T).GetTypeInfo().IsEnum)
                    {
                        if (dictionary.TryGetEnum(key, out value))
                        {
                            return(true);
                        }
                    }

                    if (objValue is string)
                    {
                        TypeCode typeCode = Type.GetTypeCode(typeof(T));

                        try
                        {
                            value = (T)Convert.ChangeType(objValue, typeCode, CultureInfo.CurrentCulture);
                            return(true);
                        }
                        catch (Exception)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(false);
        }
            /// <summary>
            /// Reads the JSON representation of the object.
            /// </summary>
            /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
            /// <param name="objectType">Type of the object.</param>
            /// <param name="existingValue">The existing value of object being read.</param>
            /// <param name="serializer">The calling serializer.</param>
            /// <returns>The object value.</returns>
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                if (reader.TokenType == JsonToken.StartObject)
                {
                    JObject valueInfo = serializer.Deserialize <JObject>(reader);
                    if (!valueInfo.TryGetValue(TypePropertyName, out JToken typeToken) ||
                        !valueInfo.TryGetValue(ValuePropertyName, out JToken valueToken))
                    {
                        // The following block is for compatability with old code behavior.
                        // The old code blindly took the first argument add treated it as the $type string,
                        // It blindly took the second argument and treated it as the $value object.
                        IEnumerator <JToken> tokenEnumerator = valueInfo.Values().GetEnumerator();
                        if (tokenEnumerator.MoveNext())
                        {
                            typeToken = tokenEnumerator.Current;
                            if (tokenEnumerator.MoveNext())
                            {
                                valueToken = tokenEnumerator.Current;
                            }
                            else
                            {
                                throw new InvalidOperationException(WebApiResources.DeserializationCorrupt());
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException(WebApiResources.DeserializationCorrupt());
                        }
                    }

                    string typeToCreate = typeToken.ToObject <string>();

                    //make sure the string is a valid type,
                    //an arbitrary type string with nested generics could overflow the
                    //stack for a DOS.
                    if (!PropertyValidation.TryGetValidType(typeToCreate, out Type type))
                    {
                        throw new InvalidOperationException(WebApiResources.DeserializationCorrupt());
                    }

                    //deserialize the type
                    return(valueToken.ToObject(type));
                }
                else if (reader.TokenType == JsonToken.Boolean ||
                         reader.TokenType == JsonToken.Bytes ||
                         reader.TokenType == JsonToken.Date ||
                         reader.TokenType == JsonToken.Float ||
                         reader.TokenType == JsonToken.Integer ||
                         reader.TokenType == JsonToken.String)
                {
                    // Allow the JSON to simply specify "name": value syntax if type information is not necessary.
                    return(serializer.Deserialize(reader));
                }
                else if (reader.TokenType == JsonToken.Null)
                {
                    return(null);
                }
                else
                {
                    throw new InvalidOperationException(WebApiResources.DeserializationCorrupt());
                }
            }
Exemple #14
0
        public ValidationOutcome Compute()
        {
            List <MetaTable> MetaTables = new List <MetaTable>();
            List <TableExtractionTaskScoreEntry> ValidationRuns = new List <TableExtractionTaskScoreEntry>();



            foreach (TableExtractionTaskScoreEntry run in task.score.TaskRuns)
            {
                executionCalls.Count(run.executionMode);
                switch (run.executionMode)
                {
                default:
                    break;

                case ExtractionTaskEngineMode.Validation:
                    ValidationRunCount++;
                    ValidationRuns.Add(run);
                    if (run.MetaTableCreated)
                    {
                        MetaTables.AddRange(run.metaTable.Where(x => x.IsValid));
                    }
                    else
                    {
                    }
                    break;
                }
            }

            if (!MetaTables.Any())
            {
                return(SetOutcome(ValidationOutcome.Invalid, "Failed to produce any meta table"));
            }

            ValidMetaTables = MetaTables.Count;

            foreach (MetaTable table in MetaTables)
            {
                PropertyCount.Learn(table.properties.Count);
                EntryCount.Learn(table.entries.Count);
                PropertyValidation.CollectProperties(table);

                var unresolved = PropertyValidation.GetUnresolved();

                foreach (MetaTableEntry entry in table.entries.items)
                {
                    foreach (MetaTableProperty property in table.properties.items)
                    {
                        metaPropertyNameCounter.Count(property.PropertyName);

                        if (unresolved.ContainsKey(property.PropertyName))
                        {
                            var prop_validation = unresolved[property.PropertyName];

                            if (property.PropertyName.isStartWithNumber())
                            {
                                prop_validation.SetOutcome(ValidationOutcome.Invalid, "Property name [" + property.PropertyName + "] starts with number");
                                continue;
                            }

                            prop_validation.ValueCounter.Count(entry.GetStoredValue(property));

                            if (entry.HasLinkedCell(property))
                            {
                                var sourceCell = entry.GetLinkedCell(property);
                                if (sourceCell.SourceNode != null)
                                {
                                    if (prop_validation.XPath.isNullOrEmpty())
                                    {
                                        prop_validation.XPath = sourceCell.SourceNode.XPath;
                                    }
                                    if (sourceCell.SourceNode.HasChildNodes)
                                    {
                                        List <HtmlNode> descendant = sourceCell.SourceNode.DescendantNodes().ToList();


                                        if (descendant.Count(x => !x.Name.StartsWith("#")) > 1)
                                        {
                                            String signature = descendant.Select(x => x.Name).toCsvInLine();

                                            prop_validation.reporter.AppendLine(sourceCell.SourceCellXPath);
                                            prop_validation.reporter.AppendLine(sourceCell.SourceNode.OuterHtml);

                                            prop_validation.SetOutcome(ValidationOutcome.Invalid, "Source cell contains [" + descendant.Count + "] descendant nodes: " + signature);
                                            continue;
                                        }
                                        else
                                        {
                                        }
                                    }
                                }
                                else
                                {
                                    prop_validation.LinkedNodes++;
                                }
                            }
                        }
                    }
                }
            }

            OutputType = TaskOutputType.data;
            if (EntryCount.Range == 0)
            {
                OutputType |= TaskOutputType.fixedEntityCount;
                if (EntryCount.Maximum == 1)
                {
                    OutputType |= TaskOutputType.singleEntity;
                }
            }
            else
            {
                OutputType |= TaskOutputType.variableEntityCount;
            }

            if (PropertyCount.Range == 0)
            {
                OutputType |= TaskOutputType.fixedPropertyCount;
            }
            else
            {
                OutputType |= TaskOutputType.variablePropertyCount;
            }

            if (OutputType.HasFlag(TaskOutputType.unstableEntityAndPropertyCounts))
            {
                SetOutcome(ValidationOutcome.Invalid, "Extraction result is unstable");
            }

            var prop_unresolved = PropertyValidation.GetUnresolved().Values.ToList();

            foreach (var prop_validation in prop_unresolved)
            {
                prop_validation.Frequency           = metaPropertyNameCounter.GetFrequencyForItem(prop_validation.item.PropertyName);
                prop_validation.DistinctValues      = prop_validation.ValueCounter.DistinctCount();
                prop_validation.SpamPropertyMeasure = 1 - prop_validation.Frequency.GetRatio(metaPropertyNameCounter.GetTopFrequency());

                /*
                 * if (prop_validation.DistinctValues == 1)
                 * {
                 *  prop_validation.SetOutcome(ValidationOutcome.Invalid, "Property had only one distinct value");
                 *  continue;
                 * }*/

                prop_validation.SetOutcome(ValidationOutcome.Validated, "");
            }



            var prop_resulrs = PropertyValidation.GetResults();



            if (prop_resulrs[ValidationOutcome.Invalid].Any())
            {
                SetOutcome(ValidationOutcome.Invalid, "[" + prop_resulrs[ValidationOutcome.Invalid].Count + "] invalid properties detected");
            }

            task.PropertyDictionary.CollectProperties(prop_resulrs[ValidationOutcome.Validated]);


            Outcome = ValidationOutcome.Validated;

            return(Outcome);
        }
 public void if_no_constraints_registered_no_exception_is_raised()
 {
     var obj = new PropertyValidation();
     obj.Validate("C");
     Expect(true);
 }
 public void if_property_name_is_null_a_domain_exception_is_raised()
 {
     var obj = new PropertyValidation();
     obj.Validate(null);
 }
 public void if_property_does_not_exists_a_domain_exception_is_raised()
 {
     var obj = new PropertyValidation();
     obj.Validate("Z");
 }