Skip to content

Instantly share code, notes, and snippets.

@snekse
Last active October 23, 2018 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snekse/3d66488591850948b4a02e0eb42c6a11 to your computer and use it in GitHub Desktop.
Save snekse/3d66488591850948b4a02e0eb42c6a11 to your computer and use it in GitHub Desktop.
Testing for a String to just be defined in Yup
// See https://runkit.com/snekse/yup-tests-string-defined/1.0.0
// and https://runkit.com/snekse/yup-tests-string-defined/2.0.0
yup.string().min(0).strict(true).nullable(true).notOneOf([undefined]);

// If we want to allow nulls, just add `.nullable(true)`
const schema = yup.string().min(0).strict(true).nullable(true).notOneOf([undefined]);

console.log(schema.isValidSync(""));
const yup = require("yup");
const okString = 'foo';
const shouldBeOkString = '';
let nullString = null;
let unString;
let emptyArr;
const schema = yup.string().min(0).strict(true).notOneOf([undefined]);
const okStringResult =
schema.label('okString').isValidSync(okString);
const shouldBeOkStringResult =
schema.label('shouldBeOkString').isValidSync(shouldBeOkString);
const nullStringResult =
schema.label('nullString').isValidSync(nullString);
const unStringResult =
schema.label('unString').isValidSync(unString);
const emptyArrResult =
schema.label('emptyArr').isValidSync(emptyArr);
console.log(`okStringResult: ${okStringResult}`);
console.log(`shouldBeOkStringResult: ${shouldBeOkStringResult}`);
console.log(`nullStringResult: ${nullStringResult}`);
console.log(`unStringResult: ${unStringResult}`);
console.log(`emptyArrResult: ${emptyArrResult}`);
const finalResult =
okStringResult &&
shouldBeOkStringResult &&
! nullStringResult &&
! unStringResult &&
! emptyArrResult;
console.log(`finalResult passes: ${finalResult}`);
const yup = require("yup");
const okString = 'foo';
const shouldBeOkString = '';
let nullString = null;
let unString;
let emptyArr;
function isDefinedString(val) {
return this.min(0).strict(true).nullable(false).notOneOf([undefined]);
}
yup.addMethod(yup.string, 'isDefined', isDefinedString)
const schema = yup.string().isDefined();
const okStringResult =
schema.label('okString').isValidSync(okString);
const shouldBeOkStringResult =
schema.label('shouldBeOkString').isValidSync(shouldBeOkString);
const nullStringResult =
schema.label('nullString').isValidSync(nullString);
const unStringResult =
schema.label('unString').isValidSync(unString);
const emptyArrResult =
schema.label('emptyArr').isValidSync(emptyArr);
console.log(`okStringResult: ${okStringResult}`);
console.log(`shouldBeOkStringResult: ${shouldBeOkStringResult}`);
console.log(`nullStringResult: ${nullStringResult}`);
console.log(`unStringResult: ${unStringResult}`);
console.log(`emptyArrResult: ${emptyArrResult}`);
const finalResult =
okStringResult &&
shouldBeOkStringResult &&
! nullStringResult &&
! unStringResult &&
! emptyArrResult;
console.log(`finalResult passes: ${finalResult}`);
@snekse
Copy link
Author

snekse commented Oct 23, 2018

If there's a better way to do this, please leave a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment