Git Product home page Git Product logo

Comments (2)

martin1cerny avatar martin1cerny commented on June 9, 2024

Hi @iwacky , Validate is not there because we didn't find a way to generate all the where functions from the schema. See this article about new and lost features in xBIM4: https://xbimteam.github.io/xbim-4/xbim-4-release-notes.html Page is still work in progress but it should give you some idea.

It is still possible to validate mandatory fields and number of items in lists. This code should do that at least:

using System;
using System.Collections;
using System.Linq;
using Xbim.Common;

namespace BasicValidation
{
    public class Validator
    {
        public static void Validate(IModel model)
        {
            var errCount = 0;
            foreach (var instance in model.Instances)
            {
                var type = instance.ExpressType;
                //check mandatory attributes
                var mProps = type.Properties.Where(p => p.Value.EntityAttribute.IsMandatory);
                foreach (var prop in mProps)
                {
                    var property = prop.Value;
                    var index = prop.Key;
                    var val = property.PropertyInfo.GetValue(instance);
                    var expVal = val as IExpressValueType;

                    if (val == null || (expVal != null && expVal.Value == null) )
                    {
                        errCount++;
                        Console.WriteLine($"#{instance.EntityLabel}={type.ExpressNameUpper}: missing attribute {property.Name} (no. {index})");
                    }
                }

                //check required number of items in lists
                var colProps = type.Properties.Where(p => p.Value.EnumerableType != null);
                foreach (var prop in colProps)
                {
                    var property = prop.Value;
                    var index = prop.Key;
                    var min = property.EntityAttribute.MinCardinality;
                    var max = property.EntityAttribute.MaxCardinality;

                    if (min > 0 || max > 0)
                    {
                        var value = property.PropertyInfo.GetValue(instance) as IList;
                        var count = value.Count;

                        //if it is uninitialized optional set continue
                        var optionalList = value as IOptionalItemSet;
                        if (optionalList != null && optionalList.Initialized == false)
                            continue;

                        if (min > 0 && count < min)
                        {
                            errCount++;
                            Console.WriteLine($"#{instance.EntityLabel}={type.ExpressNameUpper}: too few items in {property.Name} (no. {index}). Minimal count is {min}.");
                        }
                        if (max > 0 && count > max)
                        {
                            errCount++;
                            Console.WriteLine($"#{instance.EntityLabel}={type.ExpressNameUpper}: too many items in: {property.Name} (no. {index}). Maximum count is {max}.");
                        }
                    }
                }
            }
            if (errCount == 0)
                Console.WriteLine("No errors found.");
            else
                Console.WriteLine($"{errCount} errors found.");
        }
    }
}

from xbimessentials.

iwacky avatar iwacky commented on June 9, 2024

Helpful answer, thank you.

from xbimessentials.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.