question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define the schema for an array inside the object?

See original GitHub issue

How to define the schema for an array inside the object? I wanna validate the array use string and number. the first is string type, then the second is number type. like this.

const example = {test: ["this is string type", 10]};
const test = {test: ["string", 10]};

// not working
const schema={yup.object().shape({
 test: yup.array(yup.string().number()),
}}

The yup is object schema validator, it can’t array validation?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
privateOmegacommented, Oct 20, 2019

@Restoration The ideal way would have been

const yup = require("yup");

const schema = yup.object().shape({
  test: yup.array().of(yup.mixed().oneOf([yup.string(), yup.number()]))
});

// Normal Success condition
schema.validateSync({
  test: ["this is string type", 10]
});

But unfortunately yup doesn’t support one-of-type yet. So the less than ideal way I could propose is

const yup = require("yup");

const schema = yup.object().shape({
  test: yup
    .array()
    .of(
      yup.lazy(value =>
        typeof value === "number" ? yup.number() : yup.string()
      )
    )
});

// Normal Success condition
schema.validateSync({
  test: ["this is string type", 10]
});

which checks type of value before evaluating it. I think this should help you.

3reactions
mylastorecommented, Jan 9, 2021
const validationSchema = Yup.object().shape({
  myArray: Yup.array().of(
    Yup.object().shape({
      myProperty: Yup.number().required()
    })
  ),
})

myPropery??? on your example what is that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to define object in array in Mongoose schema correctly ...
You can declare trk by the following ways : - either trk : [{ lat : String, lng : String }]. or. trk...
Read more >
array — Understanding JSON Schema 2020-12 documentation
A schema can ensure that each of the items in an array is unique. Simply set the uniqueItems keyword to true . {...
Read more >
How to define object in array in Mongoose schema ... - Intellipaat
I'm currently having problems in creating a schema for the document below. The response from the server always returns the "trk" field values...
Read more >
Mongoose v6.8.1: SchemaTypes
Mongoose supports arrays of SchemaTypes and arrays of subdocuments. Arrays of SchemaTypes are also called primitive arrays, and arrays of subdocuments are also ......
Read more >
Arrays - JSON Schema
A JSON Schema for an array can be seen as set of restrictions that must apply on the elements of the array. In...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found