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

@anzenjs/joi

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@anzenjs/joi - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

cjs/src/decoder.d.ts

16

package.json
{
"name": "@anzenjs/joi",
"version": "0.0.2",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"version": "0.0.3",
"main": "cjs/src/index.js",
"types": "cjs/src/index.d.ts",
"scripts": {
"clean": "rimraf ./dist",
"clean": "rimraf ./cjs",
"prebuild": "yarn clean",
"build": "tsc --outDir dist",
"test": "mocha -r @ts-tools/node/r \"test/**/*.spec.ts?(x)\" --watch-extensions ts,tsx --colors"
"build": "ts-build . --cjs",
"test": "jest"
},

@@ -17,5 +17,7 @@ "license": "MIT",

"dependencies": {
"@anzenjs/core": "^0.0.3",
"@anzenjs/extra": "^0.0.3",
"joi": "^14.3.1"
},
"gitHead": "c82e5aa232cf6c09ee308d7f2ad2cdb5e8b0c13f"
"gitHead": "d30d062cf97b6d41cbe401b886aca6106111600e"
}

@@ -5,2 +5,3 @@ import {

reflectClass,
resolveSchema,
Schema,

@@ -11,2 +12,3 @@ SchemaType,

import Joi, { Schema as JoiSchema } from 'joi'
import { getPropertyTransformers } from './transformer'

@@ -38,3 +40,3 @@ type Transformer<T, U> = (value: T) => U

export class JoiDecoder implements IDecoder<unknown> {
private readonly resolveSchema = cache(
private readonly resolveJoiSchema = cache(
(schema: Schema): JoiSchema => {

@@ -56,15 +58,21 @@ if (this.options.schemaResolvers) {

case SchemaType.Optional:
return this.resolveSchema(schema.childSchema).optional()
return this.resolveJoiSchema(schema.childSchema).optional()
case SchemaType.Nullable:
return this.resolveSchema(schema.childSchema).allow(null)
return this.resolveJoiSchema(schema.childSchema).allow(null)
case SchemaType.List:
return this.options.joi
.array()
.items(this.resolveSchema(schema.childSchema))
.items(this.resolveJoiSchema(schema.childSchema))
case SchemaType.Dictionary:
return this.options.joi
.object()
.pattern(/.*/, this.resolveJoiSchema(schema.childSchema))
case SchemaType.Tuple:
return this.options.joi
.array()
.length(schema.childSchemas.length)
.ordered(
schema.childSchemas.map(childSchema =>
this.resolveSchema(childSchema),
this.resolveJoiSchema(childSchema),
),

@@ -91,5 +99,12 @@ )

(prev, key) => {
let joiSchema = this.resolveJoiSchema(
resolveSchema(descriptor[key]),
)
const transformers = getPropertyTransformers(ctor, key)
for (const transformer of transformers) {
joiSchema = transformer(joiSchema)
}
return {
...prev,
[key]: this.resolveSchema(descriptor[key]),
[key]: joiSchema,
}

@@ -117,3 +132,3 @@ },

case ArrayBuffer:
return this.options.joi.binary()
throw new Error('ArrayBuffer is not supported by joi yet')
case Buffer:

@@ -141,3 +156,6 @@ return this.options.joi.binary()

return (value: unknown): any => {
const result = this.resolveSchema(schema).validate(value)
const result = this.resolveJoiSchema(schema).validate(
value,
this.options.validationOptions,
)
if (result.error) {

@@ -144,0 +162,0 @@ throw result.error

@@ -15,3 +15,5 @@ import {

export const defaultJoi = Joi.defaults(schema => {
export * from './decorators'
export const commonJoi = Joi.defaults(schema => {
if (schema.schemaType === 'array') {

@@ -18,0 +20,0 @@ schema = (schema as ArraySchema).single()

{
"extends": "../../tsconfig.base.json"
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["./src"]
}
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