Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to dissable plugin_types built-in valiadations when using LY_PARSE_ONLY #2169

Closed
steweg opened this issue Feb 8, 2024 · 6 comments
Closed
Labels
is:question Issue is actually a question.

Comments

@steweg
Copy link
Contributor

steweg commented Feb 8, 2024

Hi,

is there some easy way to disable advanced leaf/leaf-list type validations when using LY_PARSE_ONLY flag?

Background: I need to somehow evaluate staff like "when" conditions prior I will get valid value from end-user. This is needed for UI layer to effectively hide the field if "when" condition is false. Assumption is that when conditions never needs the value of node itself to evaluate given expression. Of course I can generate random values based on basic types as int8 or string. But the problem will start when I have definitions like this:

    typedef multicast-ipv4-prefix {
        type inet:ipv4-prefix {
            pattern '2(2[4-9]|3[0-9])\..*';
        }
        description "The definition of IPv4 multicast prefix";
    }

For this one, I need to generate value that is not only matching original inet:ipv4-prefix, but also other custom patterns. So generating value which matches all the patterns is very problematic.

So far I have looked into the code and it seems that the main problem is that validations are not separated from storing operations. I have tried to rewrite it and split it. That worked for basic types as string. But then I find out that you have special plugins for ietf based types as ip-address, date-and-time... And for these ones, it seems that I cannot really do that easily as almost each of them has other special methods beyond within the store function, which are expecting that the basic string length/pattern validation was already completed correctly. So I can move those parts to validate function, but it would mean that if store function will end with LY_EINCOMPLETE, user must first call validate function prior functions like compare will work, which I don't really think is a good idea...

The other way around this is to introduce totally new API to create dummy data nodes by temporarily override lysc_type structure in a way that it will replace the ietf types with basic types and set all advanced validations as string patterns to NULL. Once data node is created, put everything back.

The problem is also not just problem of APIs, where I can say flag LYD_PARSE_ONLY, but also lyd_new_*

Do you have any better idea, how to achieve that?

@steweg steweg changed the title Way to dissable plugin_types built-in valiadations when using PARSE_ONLY Way to dissable plugin_types built-in valiadations when using LY_PARSE_ONLY Feb 8, 2024
@michalvasko
Copy link
Member

You have correctly observed that you cannot separate value validation and storing, the storage is often tailored to the exact value and so must be valid. Maybe describe the use-case with more details, I understand the general idea but to show/hide nodes with when you must have some input data. But you are trying to generate these data so I do not understand what is the condition exactly for showing/hiding the nodes.

@michalvasko michalvasko added the is:question Issue is actually a question. label Feb 8, 2024
@steweg
Copy link
Contributor Author

steweg commented Feb 8, 2024

Ok, in my case I am trying to develop dynamic UI which is driven by YANG schema. Let's consider simple scenario like this:

{
  leaf L1 { type string; }
  leaf L2 { type string; when "../L1= 'asd'"; }
}

In the UI, this can be represented as a form with two input fields (L1 and L2). What I want to achieve is to dynamically display or hide L2 field depending on value of L1. So when UI is first loaded, the L2 is hidden. This remains hidden till L1 value will not be changed by user to asd value. Once changed the L2 will become visible for user. If user will change L1 from asd to something else, the field L2 will become again hidden. Similarly the final value of whole form will not include value from L2 field if L1 is not asd.

The generation is only done, because I don't have real value from user at that point of time, and I need to have data node, which I can use a starting point to evaluate the xpath expression related to when schema definition. So it is kind of chicken-and-egg problem. Once I have the xpath evaluation value, I can completely remove given data node from the tree, so it will not cause any side-effects.

@michalvasko
Copy link
Member

Okay, so I am going to assume you really want L2 to be hidden in case L1 is not asd (although how will then user know such a value unhides L2?). Because if not, you could just continuously validate the input (keep a data tree with the values and print any encountered errors).

So, the problem should be reduced to how to evaluate when on L2 without an instance of L2. There is a requirement for the when conditions not to access themselves (their existence would otherwise depend on themselves) so you should be fine with just creating an opaque node instead and use that as the context node for the when XPath evaluation (this is also done internally but I do not remember the use-case).

@steweg
Copy link
Contributor Author

steweg commented Feb 8, 2024

Thanks, will definitely look into that.

@jktjkt
Copy link
Contributor

jktjkt commented Feb 13, 2024

It seems to me that you need:

  • a way to enumerate the set of nodes which are referenced from a schema node's when (and in future, must as well) statement
  • a way to plug this set of nodes to an XPath evaluator which returns a boolean

Then, whenever a value in the set of nodes that are required changes (i.e., the user types something into an input box, or they change focus to another input box), you re-evaluate all when statements of all schema nodes that are referencing the node whose value has changed, and update their visibility.

For this, I don't see the need for storing invalid data values. Am I missing something?

Also, I wonder if @bedrich-schindler might have some ideas re this use case.

@steweg
Copy link
Contributor Author

steweg commented Feb 13, 2024

I am not sure if to reopen discussion here, or be it part of #2171. The "when" part is clear and opaque nodes can be used for that purpose. Unfortunately for the other use-cases mentioned in #2171, it is not that easy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is:question Issue is actually a question.
Projects
None yet
Development

No branches or pull requests

3 participants