Skip to content

Commit

Permalink
Fix not required issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Mar 30, 2023
1 parent f3f17ef commit 05b99f7
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 11 deletions.
31 changes: 31 additions & 0 deletions sample-runs/modes/not-required-default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { buildYup } = require("../../src");

let valid;
const message = {
title: "users",
type: "object",
properties: {
username: { type: "string" }
}
};
try {
const yupSchema = buildYup(message, {
errMessages: {
username: {
required: "User is required"
}
}
});
valid = yupSchema.validateSync({
foo: "dfds"
});
// should not print
console.log("required", { valid }, yupSchema);
console.log(...yupSchema.fields.username.tests);
} catch (e) {
console.log(e);
if (e.errors) {
valid = e.errors[0];
console.log("required", { msg: valid });
}
}
15 changes: 12 additions & 3 deletions src/types/mixed/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,28 @@ class YupMixed extends Base {
return !!this.disableFlags.find((disable) => modeEntry === disable);
}

modeIsSet(modeName) {
return this.mode[modeName] !== undefined;
}

modeIsNotSet(modeName) {
return this.mode[modeName] === undefined;
}

enabledMode(modeName) {
const modeEntry = this.mode[modeName];
return !!this.enableFlags.find((disable) => modeEntry === disable);
}

get shouldPreProcessValue() {
return !this.disabledMode("notRequired");
const notRequired = this.enabledMode("notRequired") || this.modeIsNotSet("notRequired");
return notRequired
}

preProcessedConstraintValue(value) {
if (!this.shouldPreProcessValue) return value;

if (!this.isRequired(value)) {
const isExplRequired = this.isRequired(value)
if (!isExplRequired) {
return {
...value,
notRequired: true,
Expand Down
66 changes: 58 additions & 8 deletions test/validation.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
const { buildYup } = require("../src");

const invalidjson = {
const schema = {
title: "users",
type: "object",
required: ["username", "first_name", "last_name", "id_number"],
required: ["id"],
properties: {
username: { type: "string", required: true },
first_name: { type: "string", required: true },
last_name: { type: "string", required: true },
id_number: { type: "string", minLength: 12, maxLength: 13, required: true }
username: { type: "string" },
id: { type: "string", minLength: 2, maxLength: 4, required: true }
}
};
test("yup validates invalid json to return false", () => {
const yupSchema = buildYup(invalidjson);

const config = {
explNotRequired: {
mode: {
notRequired: true, // default setting
}
},
explRequired: {
mode: {
notRequired: false, // default setting
}
},
}

test("yup validates invalid username and missing id to return false", () => {
const yupSchema = buildYup(schema);
const valid = yupSchema.isValidSync({
username: 123,
foo: "bar",
Expand All @@ -21,6 +33,44 @@ test("yup validates invalid json to return false", () => {
expect(valid).toBe(false);
});

test("yup validates only id present and valid to return true", () => {
const yupSchema = buildYup(schema);
const valid = yupSchema.isValidSync({
username: "a",
id: "abc",
});
expect(valid).toBe(true);
});


test("yup validates only id present and valid and props default mode for required to return true", () => {
const yupSchema = buildYup(schema);
// console.info('mode: default', yupSchema.fields.username)
const valid = yupSchema.isValidSync({
id: "abc",
});
expect(valid).toBe(true);
});


test("yup validates only id present and valid and props expl not required to return true", () => {
const yupSchema = buildYup(schema, config.explNotRequired);
// console.info('mode: not required', yupSchema.fields.username)
const valid = yupSchema.isValidSync({
id: "abc",
});
expect(valid).toBe(true);
});

test("yup validates only id present and valid and props expl required to return false", () => {
const yupSchema = buildYup(schema, config.explRequired);
// console.info('mode: required', yupSchema.fields.username)
const valid = yupSchema.isValidSync({
id: "abc",
});
expect(valid).toBe(false);
});

const email = {
title: "users",
type: "object",
Expand Down

0 comments on commit 05b99f7

Please sign in to comment.