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

allow to add metas in the schema #341

Merged
merged 8 commits into from
Apr 21, 2024
Merged

allow to add metas in the schema #341

merged 8 commits into from
Apr 21, 2024

Conversation

thib3113
Copy link
Collaborator

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
Copy link
Collaborator Author

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 thib3113 marked this pull request as ready for review November 30, 2023 00:09
@thib3113
Copy link
Collaborator Author

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 ?

@icebob
Copy link
Owner

icebob commented Jan 13, 2024

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.

@thib3113
Copy link
Collaborator Author

thib3113 commented Apr 7, 2024

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

Copy link
Owner

@icebob icebob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

allow to add metas in the schema
2 participants