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 • 5 comments

Is it possible 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)

thib3113 avatar Oct 03 '23 11:10 thib3113

Good idea, could you create a PR?

icebob avatar Oct 08 '23 10:10 icebob

@icebob what did you think about something like that : https://github.com/thib3113/fastest-validator/commit/d37a2dc81a5f7f5c56274ac3dcaf7ce99bceae10 ?

My idea is to remove all keys starting with $$ . To mark them as "reserved" ( for you in the future, or other plugins and things like that ? )

so, we can do something like :

const schema = {
  $$description: "user",
  id: { $$description: "user Id", type: "number", positive: true, integer: true },
  address: { 
    $$description: "delivery address"
    type: "object", 
    props: {
      street: { type: "string" },
      city: { type: "string" },
      zip: { type: "string", length: 5 }
    }
  }
}

thib3113 avatar Oct 08 '23 19:10 thib3113

It can be good but it's not as easy as you think :) If you search for "$$" in the code you will see that the validator uses $$root, $$async, $$type and $$strict in different places. So you can't remove them, only others.

icebob avatar Oct 09 '23 18:10 icebob

@icebob . yes, I know .

I tryed to run the tests with this, and no tests crash (so maybe, there is something not tested) .

Else,

  • $$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

thib3113 avatar Oct 09 '23 18:10 thib3113

Could you create a PR?

icebob avatar Oct 22 '23 14:10 icebob