Skip to content

Commit

Permalink
Fix valid method (#166)
Browse files Browse the repository at this point in the history
* Add optional checkIfValidYupMethod to config

* Add null type handler
  • Loading branch information
kristianmandrup authored May 2, 2024
1 parent e63f59d commit bbb9e9b
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 24 deletions.
8 changes: 4 additions & 4 deletions Advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## <a name='Confirmpassword'></a>Confirm password

Expand Down
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "schema-to-yup",
"version": "1.12.18",
"version": "1.12.19",
"publishConfig": {
"access": "public"
},
Expand Down
11 changes: 9 additions & 2 deletions src/constraint-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -111,6 +114,10 @@ export class ConstraintBuilder extends TypeMatcher {
return false;
}

checkIfValidYupMethod(methodName) {
return !this.base[methodName];
}

yupRefMap() {
return {
string: ["length", "min", "max"],
Expand Down
2 changes: 2 additions & 0 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
toYupArray,
toYupObject,
toYupDate,
toYupNull,
} from "./types";

import { createPropertyValueResolver } from "./property-value-resolver";
Expand Down Expand Up @@ -80,6 +81,7 @@ class YupSchemaEntry extends Base {

get defaultTypeHandlerMap() {
return {
null: toYupNull,
string: toYupString,
number: toYupNumberSchemaEntry,
boolean: toYupBoolean,
Expand Down
5 changes: 4 additions & 1 deletion src/types/index.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,6 +11,8 @@ import { Base } from "./base";
export {
errValKeys,
defaults,
toYupNull,
YupNull,
YupArray,
toYupArray,
YupBoolean,
Expand All @@ -25,5 +28,5 @@ export {
toYupDate,
YupMixed,
ConvertYupSchemaError,
Base
Base,
};
15 changes: 15 additions & 0 deletions src/types/null/handler.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
7 changes: 7 additions & 0 deletions src/types/null/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NullHandler } from "./handler";

export function toYupNull(obj, config = {}) {
return obj && new NullHandler(config).handle(obj);
}

export { YupNull } from "./null";
21 changes: 21 additions & 0 deletions src/types/null/null.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 15 additions & 14 deletions src/types/schema-parser-maps/json-schema.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion test/types/number/_helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { toYupNumber, toYupNumberSchemaEntry, yup } = require("./_imports");
const { buildYup } = require("../../../src");

const isInteger = (fieldDef) =>
fieldDef && (fieldDef.type === "int" || fieldDef.type === "integer");
Expand Down Expand Up @@ -50,10 +51,10 @@ module.exports = {
createNumEntry,
createIntEntry,
createIntegerEntry,
createIntegerMinEntry,
createNumNoKeyEntry,
createSchema,
isInteger,
isNumber,
config,
buildYup,
};
8 changes: 7 additions & 1 deletion test/types/number/integer.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const { createIntEntry, createIntegerEntry, isNumber } = require("./_helpers");
const {
createIntEntry,
createIntegerEntry,
isNumber,
createSchema,
buildYup,
} = require("./_helpers");

describe("isNumber", () => {
test("int", () => {
Expand Down

0 comments on commit bbb9e9b

Please sign in to comment.