another-json-schema
Advanced tools
Comparing version 3.8.4 to 4.0.0
@@ -0,1 +1,7 @@ | ||
## 4.0.0/2024-11-11 | ||
- (breaking)rm: `ObjectId` type | ||
- add: `.trim` helper | ||
- update deps | ||
## 3.8.4/2022-05-31 | ||
@@ -2,0 +8,0 @@ |
14
index.js
@@ -184,3 +184,11 @@ const helpersFuncs = require('./helpers') | ||
let valid = true// default passed | ||
// first, check default | ||
// trim | ||
if ('trim' in ctx._children) { | ||
if (!!ctx._children.trim && (typeof value === 'string')) { | ||
value = parent[key] = value.trim() | ||
} | ||
} | ||
// check default | ||
if ('default' in ctx._children) { | ||
@@ -194,3 +202,3 @@ if (opts.default == null || opts.default) { | ||
// second, check required | ||
// check required | ||
if (ctx._children.required) { | ||
@@ -227,3 +235,3 @@ if (opts.required == null || opts.required) { | ||
for (let helper in ctx._children) { | ||
if (['type', 'default', 'required', '_customErrorMsg'].indexOf(helper) !== -1 || (opts[helper] != null && !opts[helper])) { | ||
if (['type', 'trim', 'default', 'required', '_customErrorMsg'].indexOf(helper) !== -1 || (opts[helper] != null && !opts[helper])) { | ||
continue | ||
@@ -230,0 +238,0 @@ } |
{ | ||
"name": "another-json-schema", | ||
"version": "3.8.4", | ||
"version": "4.0.0", | ||
"description": "Another JSON Schema, simple & flexible & intuitive.", | ||
@@ -20,4 +20,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"mongodb": "4.6.0", | ||
"validator": "13.7.0" | ||
"validator": "13.12.0" | ||
}, | ||
@@ -29,12 +28,11 @@ "repository": { | ||
"devDependencies": { | ||
"eslint-config-standard": "17.0.0", | ||
"eslint-plugin-import": "2.26.0", | ||
"eslint-config-standard": "17.1.0", | ||
"eslint-plugin-import": "2.31.0", | ||
"eslint-plugin-node": "11.1.0", | ||
"eslint-plugin-promise": "6.0.0", | ||
"eslint-plugin-standard": "4.1.0", | ||
"eslint-plugin-promise": "6.6.0", | ||
"intelli-espower-loader": "1.1.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^10.0.0", | ||
"istanbul": "0.4.5", | ||
"mocha": "10.8.2", | ||
"power-assert": "1.6.1" | ||
} | ||
} |
@@ -28,3 +28,3 @@ ### another-json-schema | ||
gender: { type: 'string', enum: ['male', 'female'], default: 'male' }, | ||
email: { type: 'string', isEmail: true } | ||
email: { type: 'string', trim: true, isEmail: true } | ||
}) | ||
@@ -94,3 +94,3 @@ | ||
actual: 'myEmail', | ||
expected: { type: 'string', isEmail: true }, | ||
expected: { type: 'string', trim: true, isEmail: true }, | ||
schema: 'userSchema' }, | ||
@@ -289,2 +289,4 @@ result: { name: 'nswbmw', email: 'myEmail', gender: 'male' } } | ||
- AJS.Types.Number | ||
- AJS.Types.Integer | ||
- AJS.Types.Double | ||
- AJS.Types.Date | ||
@@ -291,0 +293,0 @@ - AJS.Types.Buffer |
@@ -290,2 +290,58 @@ const AJS = require('..') | ||
it('.trim', function () { | ||
// no trim | ||
const schema = AJS('trimSchema', { | ||
email: { type: 'string', isEmail: true } | ||
}) | ||
deepEqual(schema.validate({ | ||
email: 'test@gmail.com' | ||
}), { valid: true, error: null, result: { email: 'test@gmail.com' } }) | ||
deepEqual(schema.validate({ email: 'test@gmail.com ' }), { valid: false, | ||
error: | ||
{ | ||
validator: 'isEmail', | ||
actual: 'test@gmail.com ', | ||
expected: { type: 'string', isEmail: true }, | ||
path: '$.email', | ||
schema: 'trimSchema' }, | ||
result: { email: 'test@gmail.com ' } | ||
}) | ||
// trim: false | ||
const schema2 = AJS('trimSchema', { | ||
email: { type: 'string', isEmail: true, trim: false } | ||
}) | ||
deepEqual(schema2.validate({ | ||
email: 'test@gmail.com' | ||
}), { valid: true, error: null, result: { email: 'test@gmail.com' } }) | ||
deepEqual(schema2.validate({ email: 'test@gmail.com ' }), { valid: false, | ||
error: | ||
{ | ||
validator: 'isEmail', | ||
actual: 'test@gmail.com ', | ||
expected: { type: 'string', isEmail: true, trim: false }, | ||
path: '$.email', | ||
schema: 'trimSchema' }, | ||
result: { email: 'test@gmail.com ' } | ||
}) | ||
// trim: true | ||
const schema3 = AJS('trimSchema', { | ||
email: { type: 'string', trim: true, isEmail: true } | ||
}) | ||
deepEqual(schema3.validate({ | ||
email: 'test@gmail.com' | ||
}), { valid: true, error: null, result: { email: 'test@gmail.com' } }) | ||
deepEqual(schema3.validate({ | ||
email: 'test@gmail.com ' | ||
}), { valid: true, error: null, result: { email: 'test@gmail.com' } }) | ||
const schema4 = AJS('trimSchema', { | ||
age: { type: 'number', trim: true } | ||
}) | ||
deepEqual(schema4.validate({ | ||
age: 18 | ||
}), { valid: true, error: null, result: { age: 18 } }) | ||
}) | ||
it('.default', function () { | ||
@@ -408,29 +464,2 @@ let schema = AJS('requiredSchema', { | ||
it('ObjectId', function () { | ||
function ObjectId (actual, key, parent) { | ||
if (!actual || !validator.isMongoId(actual.toString())) { | ||
return false | ||
} | ||
parent[key] = toObjectId(actual) | ||
return true | ||
} | ||
const postSchema = AJS('postSchema', { | ||
commentIds: [{ type: ObjectId }] | ||
}) | ||
const user = { | ||
commentIds: [ | ||
'111111111111111111111111', | ||
'222222222222222222222222' | ||
] | ||
} | ||
const result = postSchema.validate(user) | ||
deepEqual(result.valid, true) | ||
deepEqual(result.error, null) | ||
deepEqual(result.result.commentIds[0] instanceof toObjectId, true) | ||
deepEqual(result.result.commentIds[1] instanceof toObjectId, true) | ||
}) | ||
describe("validator's isXxx", function () { | ||
@@ -437,0 +466,0 @@ it('validator.isEmail', function () { |
@@ -5,3 +5,2 @@ const AJS = require('..') | ||
const UserSchema = AJS('User', { | ||
uid: { type: AJS.Types.ObjectId }, | ||
string: { type: AJS.Types.String }, | ||
@@ -18,11 +17,2 @@ number: { type: AJS.Types.Number }, | ||
describe('Types', function () { | ||
it('ObjectId', function () { | ||
let user = UserSchema.validate({ uid: 'xxx' }) | ||
deepEqual(user.valid, false) | ||
user = UserSchema.validate({ uid: '111111111111111111111111' }) | ||
deepEqual(user.valid, true) | ||
deepEqual(user.result.uid.toString(), '111111111111111111111111') | ||
}) | ||
it('String', function () { | ||
@@ -29,0 +19,0 @@ let user = UserSchema.validate({ string: 111 }) |
12
Types.js
const validator = require('validator') | ||
const toObjectId = require('mongodb').ObjectId | ||
@@ -8,13 +7,2 @@ const _String = String | ||
exports.ObjectID = exports.ObjectId = function ObjectId (actual, key, parent) { | ||
if (!validator.isMongoId(String(actual))) { | ||
return false | ||
} | ||
/* istanbul ignore else */ | ||
if (key != null) { | ||
parent[key] = toObjectId(actual) | ||
} | ||
return true | ||
} | ||
exports.String = function String (actual, key, parent) { | ||
@@ -21,0 +9,0 @@ /* istanbul ignore else */ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59270
1
8
1530
463
+ Addedvalidator@13.12.0(transitive)
- Removedmongodb@4.6.0
- Removed@types/node@22.10.1(transitive)
- Removed@types/webidl-conversions@7.0.3(transitive)
- Removed@types/whatwg-url@8.2.2(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbson@4.7.2(transitive)
- Removedbuffer@5.7.1(transitive)
- Removeddenque@2.1.0(transitive)
- Removedieee754@1.2.1(transitive)
- Removedip-address@9.0.5(transitive)
- Removedjsbn@1.1.0(transitive)
- Removedmemory-pager@1.5.0(transitive)
- Removedmongodb@4.6.0(transitive)
- Removedmongodb-connection-string-url@2.6.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedsaslprep@1.0.3(transitive)
- Removedsmart-buffer@4.2.0(transitive)
- Removedsocks@2.8.3(transitive)
- Removedsparse-bitfield@3.0.3(transitive)
- Removedsprintf-js@1.1.3(transitive)
- Removedtr46@3.0.0(transitive)
- Removedundici-types@6.20.0(transitive)
- Removedvalidator@13.7.0(transitive)
- Removedwebidl-conversions@7.0.0(transitive)
- Removedwhatwg-url@11.0.0(transitive)
Updatedvalidator@13.12.0