fastest-validator icon indicating copy to clipboard operation
fastest-validator copied to clipboard

allow to add metas in the schema

Open thib3113 opened this issue 2 years ago • 3 comments

Fix #338

I think it can be useful to add some metas in the schemas / rules

like :

    dot: {
        $$metas: {
          openapiName: 'dot'
        },
        $$type: "object",
        x: "number", // object props here
        y: "number", // object props here
    }

I think it can be usefull to reuse the validation params to do others things ( like here, generating openapi from params ) .

The keys starting with $$ can be just skipped from the validation, categorized as "system properties" ? (this can also allow to add other systems properties without breaking changes) .


I've checked with the already existing keys, to remove the metas after they are used

  • $$async seems to be used only in compile (so before getRuleFromSchema) / same for $$root / seems the same for $$strict
  • $$type seems to be used in resolveType (in getRuleFromSchema) only, and only on current schema ( it will call getRuleFromSchema only on sub rules ), and so I removed the $$ after

I also update typescript to allow other libraries to do things like :

declare module 'fastest-validator' {
    interface ValidationSchemaMetaKeys {
        $$metas?: {
          foo: string;
          bar: string;
        }
    }
}

TODO :

  • [ ] tests ? (the main test is to allow all other tests, without breaking them)
  • [ ] do the same on a rule

thib3113 avatar Nov 18 '23 12:11 thib3113

I found some problems with this approach ... that I will need to investigate .

First, delete will modify schema if schema is an object (reference is keep if it's an object, so, delete will delete on the reference)

Second, it seems the schema is passed to check function . It need to be passed untouched .

So, I will need to dig more on this .

thib3113 avatar Nov 26 '23 23:11 thib3113

So, @icebob . I think I'm ready .

I've added tests on each rules checking if the changes doesn't impact the rule .

Also, I'm testing it when testing another library (writings examples that use this modified version), and doesn't get bugs (feel free to tag me if you found a bug on this in the future)

Also, I've found that sometimes you will handle the schemas with care, and sometimes not (like deleting $$async on source object), not sure if it's normal ?

thib3113 avatar Nov 30 '23 00:11 thib3113

LGTM, but could you add some sentences and example about it into the docs/README.md, please?

moreover, I should check what happened with GH actions, because CI is not executed on this PR.

icebob avatar Jan 13 '24 11:01 icebob

@icebob is the little documentation added, ok/enough ?

thib3113 avatar Apr 07 '24 10:04 thib3113