Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

another-json-schema

Package Overview
Dependencies
Maintainers
0
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

another-json-schema - npm Package Compare versions

Comparing version 3.8.4 to 4.0.0

6

changelog.md

@@ -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 })

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 */

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc