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

Code generation that deduplicates validation logic #140

Closed
ggoodman opened this issue Oct 29, 2020 · 7 comments
Closed

Code generation that deduplicates validation logic #140

ggoodman opened this issue Oct 29, 2020 · 7 comments
Assignees

Comments

@ggoodman
Copy link

Use-case

Imagine you're building validator codecs for a set of inter-related schemas that define $refs on each other. The (awesome) code generation results in these being treated as totally isolated IIFE-enclosed validators. These validators duplicate the validation logic of all referenced schemas.

Request

It would be really interesting to have a facility whereby a set of inter-related schemas could be compiled as a unit and thereby produced smaller validation logic by referencing each other appropriately. I suspect that this would require major changes to how errors are collected as we would now have N validator functions instead of 1 on which errors may be added.

Anyway, schemasafe is a fantastic bit of work. Really appreciate the rigour, speed and comprehensiveness. I'm using it with https://github.com/ggoodman/json-schema-to-dts to build codegen typed validation codecs.

@ChALkeR

This comment was marked as off-topic.

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 7, 2022

Ah, disregard the previous comment, I misunderstood.

Sorry for the late reply on this.

Compiling several schemas into one object to avoid duplication is an interesting request, this should definitely be viable.
Will require some some API changes though, I will need to think about this.

ChALkeR added a commit that referenced this issue Oct 7, 2022
@ChALkeR ChALkeR self-assigned this Oct 7, 2022
@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 7, 2022

With e931d97 in place, chalker/multi-schema branch implements this as a proof-of-concept (not cleaned up)

The API will likely be different though
Also, untested yet
Also, doesn't yet automatically add everything to schemas object. Fixed

This seems to work though:

const schemas = {
  "a": {
    "type": "object",
    "properties": {
      "value": { "$ref": "b" }
    }
  },
  "b": {
    "type": "array",
    "items": { "$ref": "a" }
  }
}
const validates = validators(Object.values(schemas), { schemas })
console.log(validates[1]([]))
console.log(validates.toModule())

This also works:

const schemas = [
  {
    $id: "https://example.com/a",
    "type": "object",
    "properties": {
      "value": { "$ref": "https://example.com/b" }
    }
  },
  {
    $id: "https://example.com/b",
    "type": "array",
    "items": { "$ref": "https://example.com/a" }
  }
]
const validates = validators(schemas, { schemas })
console.log(validates[1]([]))
console.log(validates.toModule())

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 8, 2022

This also works now:

const schemas = [
  {
    $id: "https://example.com/a",
    "type": "object",
    "properties": {
      "value": { "$ref": "https://example.com/b" }
    }
  },
  {
    $id: "https://example.com/b",
    "type": "array",
    "items": { "$ref": "https://example.com/a" }
  },
  {}
]
const validates = validators(schemas)
console.log(validates[1]([]))
console.log(validates.toModule())

The API will likely change though.

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 9, 2022

The API has changed to validate(schemas, { multi: true }) instead of validators(schemas).
That'll likely be the one that will get landed.

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 10, 2022

Implemented in 1272774, now both parser() and validator() have a new { multi: true } option that allows compiling an array of schemas at once to a single module.

See example in tests.

Not properly documented for now.

@ChALkeR
Copy link
Contributor

ChALkeR commented Oct 10, 2022

Released in v1.0.0-rc.9

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

No branches or pull requests

2 participants