diff --git a/package.json b/package.json index e8eb20ba2..3da78ddef 100644 --- a/package.json +++ b/package.json @@ -119,7 +119,7 @@ "webpack-cli": "^4.8.0", "webpack-dev-middleware": "^5.0.0", "webpack-hot-middleware": "^2.25.0", - "yup": "^0.31.0" + "yup": "^1.0.0" }, "engines": { "node": "20.17.0" diff --git a/static/js/components/forms/CouponForm.js b/static/js/components/forms/CouponForm.js index 2b66e9829..d43238130 100644 --- a/static/js/components/forms/CouponForm.js +++ b/static/js/components/forms/CouponForm.js @@ -35,7 +35,7 @@ const couponValidations = yup.object().shape({ coupon_type: yup.string().required("Coupon type is required"), products: yup.array().when("is_global", { is: false, - then: yup.array().min(1, "${min} or more products must be selected"), + then: (schema) => schema.min(1, "${min} or more products must be selected"), }), is_global: yup.boolean(), activation_date: yup.date().required("Valid activation date required"), @@ -52,9 +52,8 @@ const couponValidations = yup.object().shape({ .min(1, "Must be at least ${min}") .when("discount_type", { is: DISCOUNT_TYPE_PERCENT_OFF, - then: yup - .number() - .max( + then: (schema) => + schema.max( 100, "The amount should be between (0 - 1) when discount type is percent-off.", ), @@ -62,40 +61,36 @@ const couponValidations = yup.object().shape({ discount_type: yup.string().required("Discount type is required"), max_redemptions: yup.number().when("coupon_type", { is: COUPON_TYPE_PROMO, - then: yup - .number() - .min(1, "Must be at least ${min}") - .required("Number required"), + then: (schema) => + schema.min(1, "Must be at least ${min}").required("Number required"), }), coupon_code: yup.string().when("coupon_type", { is: COUPON_TYPE_PROMO, - then: yup - .string() - .required("Coupon code is required") - .matches(/^\w+$/, "Only letters, numbers, and underscores allowed"), + then: (schema) => + schema + .required("Coupon code is required") + .matches(/^\w+$/, "Only letters, numbers, and underscores allowed"), }), num_coupon_codes: yup.number().when("coupon_type", { is: COUPON_TYPE_SINGLE_USE, - then: yup - .number() - .min(1, "Must be at least ${min}") - .required("Number required"), + then: (schema) => + schema.min(1, "Must be at least ${min}").required("Number required"), }), max_redemptions_per_user: yup.number().when("coupon_type", { is: COUPON_TYPE_PROMO, - then: yup - .number() - .required("Number required") - .min(1, "Must be at least ${min}") - .max(100, "Must be at most ${max}"), + then: (schema) => + schema + .required("Number required") + .min(1, "Must be at least ${min}") + .max(100, "Must be at most ${max}"), }), payment_transaction: yup.string().when("coupon_type", { is: COUPON_TYPE_SINGLE_USE, - then: yup.string().required("Payment transaction is required"), + then: (schema) => schema.required("Payment transaction is required"), }), payment_type: yup.string().when("coupon_type", { is: COUPON_TYPE_SINGLE_USE, - then: yup.string().required("Payment type is required"), + then: (schema) => schema.required("Payment type is required"), }), }); diff --git a/static/js/components/forms/ProfileFormFields.js b/static/js/components/forms/ProfileFormFields.js index 3f28af659..7bfc9871d 100644 --- a/static/js/components/forms/ProfileFormFields.js +++ b/static/js/components/forms/ProfileFormFields.js @@ -75,7 +75,7 @@ export const legalAddressValidation = yup.object().shape({ .label("State/Territory") .when("country", { is: includes(__, COUNTRIES_REQUIRING_STATE), - then: yup.string().required() + then: ()=>yup.string().required("State/Territory is a required field") }), country: yup .string() @@ -86,17 +86,17 @@ export const legalAddressValidation = yup.object().shape({ .string() .label("Zip/Postal Code") .trim() - .when("country", (country, schema) => { - if (country === US_ALPHA_2) { - return schema.required().matches(US_POSTAL_CODE_REGEX, { - message: - "Postal Code must be formatted as either 'NNNNN' or 'NNNNN-NNNN'" - }) - } else if (country === CA_ALPHA_2) { - return schema.required().matches(CA_POSTAL_CODE_REGEX, { - message: "Postal Code must be formatted as 'ANA NAN'" - }) - } + .when("country", { + is: US_ALPHA_2, + then: (schema)=> + schema.required().matches(US_POSTAL_CODE_REGEX, + "Postal Code must be formatted as either 'NNNNN' or 'NNNNN-NNNN'" + ) + }).when("country",{ + is: CA_ALPHA_2, + then: (schema)=>schema.required().matches(CA_POSTAL_CODE_REGEX, + "Postal Code must be formatted as 'ANA NAN'" + ) }) }) }) diff --git a/yarn.lock b/yarn.lock index 04cf7ccf8..ac3af9af9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1507,7 +1507,7 @@ __metadata: languageName: node linkType: hard -"@babel/runtime@npm:^7.1.2, @babel/runtime@npm:^7.10.5, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.4.4, @babel/runtime@npm:^7.5.5, @babel/runtime@npm:^7.7.2, @babel/runtime@npm:^7.8.4, @babel/runtime@npm:^7.8.7, @babel/runtime@npm:^7.9.2": +"@babel/runtime@npm:^7.1.2, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.4.4, @babel/runtime@npm:^7.5.5, @babel/runtime@npm:^7.7.2, @babel/runtime@npm:^7.8.4, @babel/runtime@npm:^7.8.7, @babel/runtime@npm:^7.9.2": version: 7.25.4 resolution: "@babel/runtime@npm:7.25.4" dependencies: @@ -8091,7 +8091,7 @@ __metadata: languageName: node linkType: hard -"lodash-es@npm:^4.17.11, lodash-es@npm:^4.17.14, lodash-es@npm:^4.2.1": +"lodash-es@npm:^4.17.14, lodash-es@npm:^4.2.1": version: 4.17.21 resolution: "lodash-es@npm:4.17.21" checksum: 05cbffad6e2adbb331a4e16fbd826e7faee403a1a04873b82b42c0f22090f280839f85b95393f487c1303c8a3d2a010048bf06151a6cbe03eee4d388fb0a12d2 @@ -8911,7 +8911,7 @@ __metadata: webpack-cli: ^4.8.0 webpack-dev-middleware: ^5.0.0 webpack-hot-middleware: ^2.25.0 - yup: ^0.31.0 + yup: ^1.0.0 languageName: unknown linkType: soft @@ -10054,7 +10054,7 @@ __metadata: languageName: node linkType: hard -"property-expr@npm:^2.0.4": +"property-expr@npm:^2.0.5": version: 2.0.6 resolution: "property-expr@npm:2.0.6" checksum: 89977f4bb230736c1876f460dd7ca9328034502fd92e738deb40516d16564b850c0bbc4e052c3df88b5b8cd58e51c93b46a94bea049a3f23f4a022c038864cab @@ -12324,6 +12324,13 @@ __metadata: languageName: node linkType: hard +"tiny-case@npm:^1.0.3": + version: 1.0.3 + resolution: "tiny-case@npm:1.0.3" + checksum: 3f7a30c39d5b0e1bc097b0b271bec14eb5b836093db034f35a0de26c14422380b50dc12bfd37498cf35b192f5df06f28a710712c87ead68872a9e37ad6f6049d + languageName: node + linkType: hard + "tiny-invariant@npm:^1.0.2": version: 1.3.3 resolution: "tiny-invariant@npm:1.3.3" @@ -12509,6 +12516,13 @@ __metadata: languageName: node linkType: hard +"type-fest@npm:^2.19.0": + version: 2.19.0 + resolution: "type-fest@npm:2.19.0" + checksum: a4ef07ece297c9fba78fc1bd6d85dff4472fe043ede98bd4710d2615d15776902b595abf62bd78339ed6278f021235fb28a96361f8be86ed754f778973a0d278 + languageName: node + linkType: hard + "type-is@npm:~1.6.18": version: 1.6.18 resolution: "type-is@npm:1.6.18" @@ -13579,15 +13593,14 @@ __metadata: languageName: node linkType: hard -"yup@npm:^0.31.0": - version: 0.31.1 - resolution: "yup@npm:0.31.1" +"yup@npm:^1.0.0": + version: 1.4.0 + resolution: "yup@npm:1.4.0" dependencies: - "@babel/runtime": ^7.10.5 - lodash: ^4.17.20 - lodash-es: ^4.17.11 - property-expr: ^2.0.4 + property-expr: ^2.0.5 + tiny-case: ^1.0.3 toposort: ^2.0.2 - checksum: 79bf28d6afa4cebcabe3322bb82eec93632289cad47dbe47a89ae067f4438b3a7de8ac2e30977539727510450a4f73ed9051c6d6aa2718d9f1ba6b345179cb7d + type-fest: ^2.19.0 + checksum: 20a2ee0c1e891979ca16b34805b3a3be9ab4bea6ea3d2f9005b998b4dc992d0e4d7b53e5f4d8d9423420046630fb44fdf0ecf7e83bc34dd83392bca046c5229d languageName: node linkType: hard