How to use the not function from joi

Find comprehensive JavaScript joi.not code examples handpicked from public code repositorys.

joi.not is a method in the Joi validation library that negates a given validation schema.

159
160
161
162
163
164
165
166
167
168
      PARTY_TYPES.partnershipBBA,
      PARTY_TYPES.partnershipOtherThanTaxMatters,
    ),
  otherwise: joi.optional().allow(null),
  then: joi.when('orderForCds', {
    is: joi.not(true),
    otherwise: joi.optional().allow(null),
    then: joi.required(),
  }),
}),
fork icon36
star icon72
watch icon19

+ 3 other calls in file

61
62
63
64
65
66
67
68
69
70
// 重置密码
exports.edit_password_schema = joi.object().keys({
  user_id,
  // 使用 password 这个规则,验证 req.body.oldPwd 的值
  old_password: password,
  // 使用 joi.not(joi.ref('oldPwd')).concat(password) 规则,验证 req.body.newPwd 的值
  // 解读:
  // 1. joi.ref('oldPwd') 表示 newPwd 的值必须和 oldPwd 的值保持一致
  // 2. joi.not(joi.ref('oldPwd')) 表示 newPwd 的值不能等于 oldPwd 的值
  // 3. .concat() 用于合并 joi.not(joi.ref('oldPwd')) 和 password 这两条验证规则
fork icon1
star icon2
watch icon1

+ 3 other calls in file

How does joi.not work?

joi.not is a method in the Joi validation library that negates a given validation schema. When you call joi.not(), you pass in a Joi schema as its parameter. The method returns a new schema that negates the original schema, meaning that it will validate values that do not match the original schema. For example, if you have a schema that validates strings that contain only letters, you can use joi.not() to create a new schema that validates strings that contain anything other than letters. The joi.not() method can be used in conjunction with other Joi validation methods to create more complex validation schemas. For example, you could use joi.object() to define an object schema and then use joi.not() to negate a particular property validation. Overall, joi.not() provides a way to negate a validation schema and create more flexible validation rules for your application's data.

50
51
52
53
54
55
56
57
58
59
60
    // 使用 joi.not(joi.ref('oldPwd')).concat(password) 规则,验证 req.body.newPwd 的值
    // 解读:
    // 1. joi.ref('oldPwd') 表示 newPwd 的值必须和 oldPwd 的值保持一致
    // 2. joi.not(joi.ref('oldPwd')) 表示 newPwd 的值不能等于 oldPwd 的值
    // 3. .concat() 用于合并 joi.not(joi.ref('oldPwd')) 和 password 这两条验证规则
    newPwd: joi.not(joi.ref('oldPwd')).concat(password),
  },
}


// 验证规则对象 - 更新头像
fork icon0
star icon1
watch icon1

+ 35 other calls in file

51
52
53
54
55
56
57
58
59
60
61
    // 使用 joi.not(joi.ref('oldPwd')).concat(password) 规则,验证 req.body.newPwd 的值
    // 解读:
    // 1. joi.ref('oldPwd') 表示 newPwd 的值必须和 oldPwd 的值保持一致
    // 2. joi.not(joi.ref('oldPwd')) 表示 newPwd 的值不能等于 oldPwd 的值
    // 3. .concat() 用于合并 joi.not(joi.ref('oldPwd')) 和 password 这两条验证规则
    newPwd: joi.not(joi.ref('oldPwd')).concat(password)
  }
}



fork icon0
star icon0
watch icon1

+ 23 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const Joi = require("joi");

const schema = Joi.object({
  name: Joi.string().required(),
  age: Joi.number().positive().required(),
});

const negatedSchema = Joi.object({
  name: Joi.not(Joi.string().required()),
  age: Joi.not(Joi.number().positive().required()),
});

const user = {
  name: 123,
  age: -10,
};

const result1 = schema.validate(user);
console.log(result1.error); // Output: ValidationError: "name" must be a string

const result2 = negatedSchema.validate(user);
console.log(result2.error); // Output: undefined

In this example, we first define a schema using Joi.object() that requires a name property that is a string and an age property that is a positive number. We then define a second schema using Joi.object() that negates the name and age properties of the original schema using Joi.not(). This means that the negated schema will validate objects where the name property is not a string and the age property is not a positive number. Next, we define a user object that fails the validation of the original schema since the name property is a number and the age property is negative. We then validate the user object against both schemas using the validate() method. The first schema validation will return an error because the name property is not a string, but the second schema validation will pass because the name and age properties do not match the negated schema. Overall, this example demonstrates how to use joi.not() to negate a validation schema and create more flexible validation rules for your application's data.

39
40
41
42
43
44
45
46
47
48
// 重置密码的规则对象
exports.update_password_schema = {
    body: {
        // 使用password 这个规则,验证req.body.oldPwd 的值
        oldPwd: password,
        // 使用 joi.not(joi.ref('oldPwd')).concat(password)规则,验证req.body.newPwd
        // 解读:
        // 1.joi.ref('oldPwd')表示newPwd的值必须和oldPwd的值保持一致
        // 2.joi.not(joi.ref('oldPwd')) 表示newPwd的值不能等于oldPwd的值
        // 3. .concat(password)用于合并joi.not(joi.ref('oldPwd'))和password这两条验证规则
fork icon0
star icon0
watch icon1

52
53
54
55
56
57
58
59
60
61
62
    oldPassword: password,
    
    // 1. joi.ref('oldPwd') 表示 newPwd 的值必须和 oldPwd 的值保持一致
    // 2. joi.not(joi.ref('oldPwd')) 表示 newPwd 的值不能等于 oldPwd 的值
    // 3. .concat() 用于合并 joi.not(joi.ref('oldPwd')) 和 password 这两条验证规则
    newPassword: joi.not(joi.ref('oldPwd')).concat(password)
  }
}


// 验证规则对象 - 更新头像
fork icon0
star icon0
watch icon2

+ 8 other calls in file