Coder Social home page Coder Social logo

Comments (5)

kristianmandrup avatar kristianmandrup commented on August 17, 2024

Never heard about or used this JSON schema feature. Please write a failing spec (test) with the setup requested and the expected result and make a PR. You can also use the existing infra to add your own logic to override or extend the default.

For example, one can allow additional properties, but only if they are each a string:

{
  "type": "object",
  "properties": {
    "number":      { "type": "number" },
    "street_name": { "type": "string" },
    "street_type": { "type": "string",
                     "enum": ["Street", "Avenue", "Boulevard"]
                   }
  },
  "additionalProperties": { "type": "string" }
}

How would this example ideally be reflected using Yup? We could down reverse engineer from there...

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

I've added an empty test for additionalProperties. Please have a go at it ;)
You could extend YupBuilder

class MyYupBuilder extends YupBuilder {
  constructor(schema, config = {}) {
    super(schema, config);
    this.additionalProps = schema.additionalProperties
  }

  propsToShape(opts) {
    const shape = super.propsToShape(opts)
    const { additionalProps } = this
    if (additionalProps) {
      // modify yup shape
    }
    return shape
  }
}

Figure out what yup shape would work in this case, then use the recipe above :) Would love to see a PR for this.

BTW: Just fixed the library core so it should work again. The new ConstraintBuilder had messed the internals up a bit and was more difficult to get right than anticipated. Sorry about that.

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

In the latest commit I've made a slight change to the YupBuilder to make this easier (more flexible infra)

  constructor(...) {
    this.additionalProps = this.getAdditionalProperties(schema);
  }

  propsToShape(opts = {}) {
    const shape = this.objPropsToShape(opts);
    this.objPropsShape = shape;
    this.addPropsShape = this.additionalPropsToShape(opts, shape);
    return shape;
  }

  additionalPropsToShape(opts, shape) {
    // do your magic here using this.additionalProps 
   // make new yup constraint function calls on the incoming yup shape object
    return shape;
  }

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

I think you could use the yup test method to add this extra functionality

let jimmySchema = string().test(
  'is-jimmy',
  '${path} is not Jimmy',
  value => value === 'jimmy',
);

The test function would then need a scope with access to the property keys traversed/used as part of the normal validation to check if this property is an additional property not otherwise covered in this context.

const propNamesUsed = Object.keys(this.properties)
const additionalPropertyTestFn = (name, errorFn, (value) => {
  if (propNamesUsed.includes(name)) return true; // valid prop if already used
  // test logic here

  if (value.type == 'string') {
    const schema = toYupString(schemaValue)
    schema.validateSync(
 )   

  return true
})
shape.test = additionalPropertyTestFn

Perhaps better to use this variant of test

const propNamesUsed = Object.keys(this.properties)
const schemaValue = this.additionalProperties
const additionalPropertyTestFn = (opts) => {
  if (propNamesUsed.includes(name)) return true; // valid prop if already used

  const args = createSchemaOpts(opts, schemaValue)

  // test logic here (ideally generalise to support all supported types)
  if (value.type == 'string') {
    const schema = toYupString(...args)
    return validate(schema, value)
 )   

  return true
})
shape.test = additionalPropertyTestFn

from schema-to-yup.

kristianmandrup avatar kristianmandrup commented on August 17, 2024

I've now added a note on this to the Readme of the library

from schema-to-yup.

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.