Validation of a string or number that is not performing as expected

I have been working with the yup library for JavaScript data validation, but I am encountering unexpected behavior. Despite thoroughly reviewing their documentation, I cannot pinpoint where I am misusing their API. When I run the unit test below, it fails when I expect it to pass. Any suggestions on what might be causing this issue?

import { object, string } from 'yup';

test('validator', async () => {
  let schema = object({
    name: string(),
  });

  expect(await schema.isValid({ name: 123 })).toEqual(false);
});

I am using version

"yup": "^0.32.11"

I attempted to run this validation within a Jest unit test and it failed. I also looked into other validation methods in their API docs.

Answer №1

The outcome follows the expected pattern due to the implementation of the yup library. Utilizing the strict option in a schema prevents the execution of the yup parser, thereby eliminating the chance of converting your number value into a string value through type coercion. When the strict option is enabled, the number value of 123 will undergo validation without type coercion. Consequently, since it is not a string value, the validation will be unsuccessful. In contrast, when the strict option is disabled, type coercion takes place resulting in successful validation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When moving from Babel version 5.8.35 to 6.0.0, be prepared for app.js to throw a SyntaxError and encounter an unexpected token during compilation

Currently, I am in the process of enhancing my ReactJS components using webpack. However, I have encountered a hurdle while trying to transition from babel version 5 to 6. Upon attempting the upgrade, it resulted in a stack trace error within my app.js cl ...

Click on the navigation bar buttons to toggle between different divs and display multiple information boxes simultaneously

Here is the current code snippet... $("#contact").click(function() { if ( $( "#contact_div" ).length ) { $("#contact_div").remove(); } else { var html='<div id="contact_div" class="contact-info"><p>Contact info</p&g ...

Unlimited digest loop in Angular.js caused by nested ng-repeat and filter

In my AngularJS project, I have developed a custom filter that categorizes elements by type and performs a search for multiple search terms across all attributes of devices. angular.module('abc').filter('searchFor', function(){ return ...

Enhancing UI Design with Styled-Components and Material Components using Media Queries

Struggling to grasp media queries with MUI and styled components. Take a look at the following syntax using styled-components: Syntax 1: const Video = styled.video` width: 860px; @media ${device.mobileSM} { width: 90%; } `; Additionally, there ...

Ensure Next.js retains the route when moving from one screen to another

I am currently facing a challenge in Next.js while creating a Dashboard. The root route for this dashboard would be: /dashboard/ Within this route, users can select different stores to access their respective dashboards. When a user clicks on a specific s ...

Is there a way to transfer JavaScript data to PHP?

<div> <p>This is a sample HTML code with JavaScript for tallying radio button values and passing them to PHP via email.</p> </div> If you need help converting JavaScript data to PHP and sending it via email, there are v ...

The file could not be loaded as a result of Multipart: The boundary was not detected

Having trouble uploading images from my desktop due to a multipart boundary error. How can I set a proper boundary for image uploading? Any advice would be greatly appreciated as this is my first time attempting image uploads. Implementing an HTML event l ...

Transforming the code from numerous div elements to utilizing Ajax for efficient execution

One of the key functionalities is that the content within the <div> changes based on the option selected in the <select> element. I am looking to incorporate ajax instead of multiple <div> elements. However, my proficiency with ajax is l ...

Adding dynamic metadata to a specific page in a next.js app using the router

I was unable to find the necessary information in the documentation, so I decided to seek help here. My goal is to include metadata for my blog posts, but I am struggling to figure out how to do that. Below is a shortened version of my articles/[slug]/page ...

What causes the issue of text being blocked by a webgl shader background even when the text div is layered above and given a higher z-index?

I am looking to enhance a WordPress theme by dynamically generating glsl shaders using the three.js library for JavaScript, and then incorporating those shaders into various HTML elements, such as using them as backgrounds for text content. My current cha ...

The background image causes the scrollbar to vanish

As a beginner, I am in the process of creating a web page that features a consistent background image. However, I have encountered an issue where the scroll bar does not appear on a specific page called "family details" due to the background image. I atte ...

Deactivate specific ASP.Net validation controls upon checking a checkbox

Currently, I am utilizing the traditional ASP.NET validation system for a checkout procedure. Within this process, there is a specific checkbox labeled "I'll call with my credit card details". When this checkbox is selected, it should trigger the disa ...

Effortless Android JSON Decoding

Hey there, I'm new to android development and trying to figure out how to parse JSON in an android activity without using PHP. Instead, I am utilizing NODEJS on a server in AWS to generate my JSON data. I have a JSON object with values {"1":"Hello", ...

What is the best way to transform api.Response into a data frame or json format using Python?

Looking at the data I have, my goal is to transform it into a data frame. t = cmc.globalmetrics_quotes_latest() (Cmc refers to the coinmarketcap api) type(t)= coinmarketcapapi.response """ RESPONSE: 820ms OK: {'active_cryptocurrencies ...

Is Cognito redirect causing issues with Angular router responsiveness?

When employing social login via AWS Cognito, Cognito sends a redirect to the browser directing it to the signin redirect URL after signing in. In this case, the specified URL is http://localhost:4200/home/. Upon receiving this redirect, the application in ...

Filtering React component names and their corresponding values

I am looking to apply filters to React components based on their name and values. Below is the array that needs to be filtered: let filteredArray: any[] = [] const filteredItems: any[] = eventList.filter( (event) => event.printEvent.labels = ...

Translating jsonObject into a Map in the Scala programming language

I am currently coding in Scala. I am looking for a way to convert a JSON object into a Map containing String keys and values. Here is an example of my jsonObject: {"Key1":"Val1","Key2":"Val2","Key3":"Val3"} I would like to transform it into a Map like ...

Unable to assign value to Ref data property in Vue3 due to undefined item

Recently, I've been utilizing Vue3 and Nuxt3 to work on a project. My main task involves extracting the :id parameter from the URL and checking if it matches an ID in a JSON file. If there is a match, I update a reference data point called 'exist ...

What role does the "deep" parameter serve when declaring a watcher in VueJS?

I recently discovered a feature in Vue.js called watchers while working on my web app. As I was exploring the API documentation, I came across a flag known as deep. This flag caught my attention because it defaults to false. I'm curious to know what s ...

Challenges in handling asynchronous data within static JSON objects in Angular2

I have a service set up with some static objects that are being utilized in my UI. fetchRulesVariables() fetchRuleVariables() { let variables = [ { name: 'Credit Funding Type', id: 1, multiple: ...