// 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(""));
Last active
October 23, 2018 18:21
-
-
Save snekse/3d66488591850948b4a02e0eb42c6a11 to your computer and use it in GitHub Desktop.
Testing for a String to just be defined in Yup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If there's a better way to do this, please leave a comment.