Code Monkey home page Code Monkey logo

Comments (4)

tlivings avatar tlivings commented on August 26, 2024

This is probably going to be a documented limitation of how allOf will work with Joi. allOf is not a combination of the defined schemas, but rather a validation against all provided schemas.

To allow this to work, allOf must always allow additional properties so that it may continue checking.

With access to more of the job error API we could get more custom about avoiding this sort of decision making but for now it's probably harder.

Will think through it some more.

from enjoi.

ozum avatar ozum commented on August 26, 2024

Oh, I missed that. It seems also a limitation of JSON Schema too, and this exact example is given in doc's combining schemas section

It may be added in next versions of JSON Schema...

This shortcoming is perhaps one of the biggest surprises of the combining operations in JSON schema: it does not behave like inheritance in an object-oriented language. There are some proposals to address this in the next version of the JSON schema specification.

from enjoi.

ozum avatar ozum commented on August 26, 2024

As a workaround, in my particular case at hand, I will try to add an optional preprocess step to convert allOf to non cascading schema. (They are not equivalent as per JSON Schema spec, but equal for my needs)

Using this function:

// CAVEAT: This is NOT a general solution, just a workaround, and not thoroughly tested.
function flattenAllOf(schema) {
  if (!schema.allOf) {
    return schema;
  }

  let properties = schema.properties || {};
  schema.allOf.forEach(element => {
    properties = { ...properties, ...flattenAllOf(element).properties };
  });

  return {
    type: "object",
    properties,
  };
}

From:

{
  allOf: [
    {
      allOf: [
        {
          type: "object",
          properties: {
            a: { type: "string" },
            b: { type: "string" },
          },
        },
        {
          type: "object",
          properties: {
            c: { type: "string" },
            d: { type: "string" },
          },
        },
      ],
    },
  ],
};

to

{
  type: "object",
  properties: {
    a: { type: "string" },
    b: { type: "string" },
    c: { type: "string" },
    d: { type: "string" },
  },
},

from enjoi.

tlivings avatar tlivings commented on August 26, 2024

Cool, and thanks for the feedback!

I appreciate the issues - helps me get insight into things I missed.

from enjoi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.