@anzenjs/joi
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"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"] | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
277881
54
2256
3
1
+ Added@anzenjs/core@^0.0.3
+ Added@anzenjs/extra@^0.0.3
+ Added@anzenjs/core@0.0.3(transitive)
+ Added@anzenjs/extra@0.0.3(transitive)