From bbb9e9bd92e4be9d26bbb58e424e5363521a0eba Mon Sep 17 00:00:00 2001 From: Kristian Mandrup Date: Thu, 2 May 2024 07:11:09 +0200 Subject: [PATCH] Fix valid method (#166) * Add optional checkIfValidYupMethod to config * Add null type handler --- Advanced.md | 8 +++--- Changelog.md | 4 +++ package.json | 2 +- src/constraint-builder.js | 11 ++++++-- src/entry.js | 2 ++ src/types/index.js | 5 +++- src/types/null/handler.js | 15 +++++++++++ src/types/null/index.js | 7 +++++ src/types/null/null.js | 21 +++++++++++++++ src/types/schema-parser-maps/json-schema.js | 29 +++++++++++---------- test/types/number/_helpers.js | 3 ++- test/types/number/integer.test.js | 8 +++++- 12 files changed, 91 insertions(+), 24 deletions(-) create mode 100644 src/types/null/handler.js create mode 100644 src/types/null/index.js create mode 100644 src/types/null/null.js diff --git a/Advanced.md b/Advanced.md index c2489d0..426317a 100644 --- a/Advanced.md +++ b/Advanced.md @@ -31,10 +31,10 @@ This library is built to be easy to customize or extend to suit individual devel - [Custom builder](#custom-builder) - [Custom builder functions](#custom-builder-functions) -- [entry builders](#custom-entry-builders) -- [type handlers](#custom-type-handlers) -- [constraint handler functions](#custom-constraint-handler-functions) -- [constraint builder](#custom-constraint-builder) +- [Custom entry builders](#custom-entry-builders) +- [Custom type handlers](#custom-type-handlers) +- [Custom constraint handler functions](#custom-constraint-handler-functions) +- [Custom constraint builder](#custom-constraint-builder) ## Confirm password diff --git a/Changelog.md b/Changelog.md index 1dada15..08c0c2f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog +# 1.12.19 + +Add optional `checkIfValidYupMethod` to `config` object to let user control this check if needed. + # 1.11.18 Fixes `itemsOf` for array type. diff --git a/package.json b/package.json index c3e9a0f..b22e72a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "schema-to-yup", - "version": "1.12.18", + "version": "1.12.19", "publishConfig": { "access": "public" }, diff --git a/src/constraint-builder.js b/src/constraint-builder.js index c18a045..eff5c67 100644 --- a/src/constraint-builder.js +++ b/src/constraint-builder.js @@ -62,9 +62,12 @@ export class ConstraintBuilder extends TypeMatcher { } const yupConstraintMethodName = this.aliasMap[method] || method; + const checkIfValidYupMethod = ( + this.config.checkIfValidYupMethod || this.checkIfValidYupMethod + ).bind(this); - if (!yup[yupConstraintMethodName]) { - const msg = `Yup has no such API method: ${yupConstraintMethodName}`; + if (!checkIfValidYupMethod(yupConstraintMethodName, this)) { + const msg = `Current base has no such API method: ${yupConstraintMethodName}`; this.warn(msg); return false; } @@ -111,6 +114,10 @@ export class ConstraintBuilder extends TypeMatcher { return false; } + checkIfValidYupMethod(methodName) { + return !this.base[methodName]; + } + yupRefMap() { return { string: ["length", "min", "max"], diff --git a/src/entry.js b/src/entry.js index 1266bb6..fb6cb53 100644 --- a/src/entry.js +++ b/src/entry.js @@ -6,6 +6,7 @@ import { toYupArray, toYupObject, toYupDate, + toYupNull, } from "./types"; import { createPropertyValueResolver } from "./property-value-resolver"; @@ -80,6 +81,7 @@ class YupSchemaEntry extends Base { get defaultTypeHandlerMap() { return { + null: toYupNull, string: toYupString, number: toYupNumberSchemaEntry, boolean: toYupBoolean, diff --git a/src/types/index.js b/src/types/index.js index a8d7076..baf6660 100644 --- a/src/types/index.js +++ b/src/types/index.js @@ -1,4 +1,5 @@ import { YupMixed, ConvertYupSchemaError, errValKeys, defaults } from "./mixed"; +import { YupNull, toYupNull } from "./string"; import { YupArray, toYupArray } from "./array"; import { YupBoolean, toYupBoolean } from "./boolean"; import { YupNumber, toYupNumber, toYupNumberSchemaEntry } from "./number"; @@ -10,6 +11,8 @@ import { Base } from "./base"; export { errValKeys, defaults, + toYupNull, + YupNull, YupArray, toYupArray, YupBoolean, @@ -25,5 +28,5 @@ export { toYupDate, YupMixed, ConvertYupSchemaError, - Base + Base, }; diff --git a/src/types/null/handler.js b/src/types/null/handler.js new file mode 100644 index 0000000..adea9ae --- /dev/null +++ b/src/types/null/handler.js @@ -0,0 +1,15 @@ +import { YupNull } from "./null"; + +export class NullHandler { + constructor(config) { + this.config = config; + } + + isBoolean(obj) { + return this.config.isNull(obj); + } + + handle(obj) { + return this.isNull(obj) && YupNull.create(obj).createSchemaEntry(); + } +} diff --git a/src/types/null/index.js b/src/types/null/index.js new file mode 100644 index 0000000..ca819d2 --- /dev/null +++ b/src/types/null/index.js @@ -0,0 +1,7 @@ +import { NullHandler } from "./handler"; + +export function toYupNull(obj, config = {}) { + return obj && new NullHandler(config).handle(obj); +} + +export { YupNull } from "./null"; diff --git a/src/types/null/null.js b/src/types/null/null.js new file mode 100644 index 0000000..6da95b9 --- /dev/null +++ b/src/types/null/null.js @@ -0,0 +1,21 @@ +import { YupMixed } from "../mixed"; + +export class YupNull extends YupMixed { + constructor(obj) { + super(obj); + this.type = this.baseType; + this.base = this.validatorInstance; + } + + get baseType() { + return "null"; + } + + get validatorInstance() { + return this.validator.null(); + } + + static create(obj) { + return new YupNull(obj); + } +} diff --git a/src/types/schema-parser-maps/json-schema.js b/src/types/schema-parser-maps/json-schema.js index b1f281a..986c21b 100644 --- a/src/types/schema-parser-maps/json-schema.js +++ b/src/types/schema-parser-maps/json-schema.js @@ -1,19 +1,20 @@ const defaults = { - getProps: obj => obj && obj.properties, - getType: obj => obj && obj.type, - getName: obj => obj && (obj.name || obj.title), - getConstraints: obj => obj, - isString: obj => obj && obj.type === "string", - isArray: obj => obj && obj.type === "array", - isInteger: obj => obj && (obj.type === "integer" || obj.type === "int"), - isBoolean: obj => obj && obj.type === "boolean", - hasDateFormat: obj => - obj && ["date", "date-time"].find(t => t === obj.format), - isDate: obj => + getProps: (obj) => obj && obj.properties, + getType: (obj) => obj && obj.type, + getName: (obj) => obj && (obj.name || obj.title), + getConstraints: (obj) => obj, + isString: (obj) => obj && obj.type === "string", + isArray: (obj) => obj && obj.type === "array", + isInteger: (obj) => obj && (obj.type === "integer" || obj.type === "int"), + isBoolean: (obj) => obj && obj.type === "boolean", + isNull: (obj) => obj && obj.type === "null", + hasDateFormat: (obj) => + obj && ["date", "date-time"].find((t) => t === obj.format), + isDate: (obj) => obj && obj.type === "string" && defaults.hasDateFormat(obj.format), - isNumber: obj => obj && (obj.type === "number" || defaults.isInteger(obj)), - isObject: obj => obj && obj.type === "object", - isRequired: obj => obj && obj.required + isNumber: (obj) => obj && (obj.type === "number" || defaults.isInteger(obj)), + isObject: (obj) => obj && obj.type === "object", + isRequired: (obj) => obj && obj.required, }; export default defaults; diff --git a/test/types/number/_helpers.js b/test/types/number/_helpers.js index 4381bbd..a37fab3 100644 --- a/test/types/number/_helpers.js +++ b/test/types/number/_helpers.js @@ -1,4 +1,5 @@ const { toYupNumber, toYupNumberSchemaEntry, yup } = require("./_imports"); +const { buildYup } = require("../../../src"); const isInteger = (fieldDef) => fieldDef && (fieldDef.type === "int" || fieldDef.type === "integer"); @@ -50,10 +51,10 @@ module.exports = { createNumEntry, createIntEntry, createIntegerEntry, - createIntegerMinEntry, createNumNoKeyEntry, createSchema, isInteger, isNumber, config, + buildYup, }; diff --git a/test/types/number/integer.test.js b/test/types/number/integer.test.js index 9e2847f..6c9608c 100644 --- a/test/types/number/integer.test.js +++ b/test/types/number/integer.test.js @@ -1,4 +1,10 @@ -const { createIntEntry, createIntegerEntry, isNumber } = require("./_helpers"); +const { + createIntEntry, + createIntegerEntry, + isNumber, + createSchema, + buildYup, +} = require("./_helpers"); describe("isNumber", () => { test("int", () => {