@orion-js/schema 3.0.17 → 3.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018 Orionjs Team
3
+ Copyright (c) 2021 Orionjs Team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,2 +1,2 @@
1
1
  import { CurrentNodeInfoOptions, Schema } from '../types/schema';
2
- export default function clean(schema: Schema, doc?: {}, opts?: CurrentNodeInfoOptions, ...args: any[]): Promise<Schema>;
2
+ export default function clean(schema: Schema | Function, doc?: {}, opts?: CurrentNodeInfoOptions, ...args: any[]): Promise<Schema>;
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getSchemaFromTypedModel_1 = require("../getSchemaFromTypedModel");
6
7
  const recursiveClean_1 = __importDefault(require("./recursiveClean"));
7
8
  const defaultOptions = {
8
9
  autoConvert: true,
@@ -11,6 +12,7 @@ const defaultOptions = {
11
12
  removeEmptyStrings: false
12
13
  };
13
14
  async function clean(schema, doc = {}, opts = {}, ...args) {
15
+ schema = (0, getSchemaFromTypedModel_1.getSchemaFromTypedModel)(schema);
14
16
  const options = { ...defaultOptions, ...opts };
15
17
  const params = {
16
18
  schema: { type: schema },
@@ -467,3 +467,16 @@ test('pass currentDoc cleaning complex schemas', async () => {
467
467
  // @ts-ignore TODO: Check why the __clean method is being used here instead of clean
468
468
  await (0, index_1.default)(schema, doc);
469
469
  });
470
+ test('On blackbox allow use of custom clean', async () => {
471
+ const schema = {
472
+ info: {
473
+ type: 'blackbox',
474
+ optional: true,
475
+ async clean(info, { doc }) {
476
+ return { hello: 'world' };
477
+ }
478
+ }
479
+ };
480
+ const result = await (0, index_1.default)(schema, { info: { hello: 'night' } });
481
+ expect(result).toEqual({ info: { hello: 'world' } });
482
+ });
@@ -8,6 +8,7 @@ const isArray_1 = __importDefault(require("lodash/isArray"));
8
8
  const cleanType_1 = __importDefault(require("./cleanType"));
9
9
  const isNil_1 = __importDefault(require("lodash/isNil"));
10
10
  const getObjectNode_1 = __importDefault(require("./getObjectNode"));
11
+ const convertTypedModel_1 = require("../getValidationErrors/convertTypedModel");
11
12
  const cleanObjectFields = async function ({ schema, value, ...other }) {
12
13
  const keys = Object.keys(schema.type).filter(key => !key.startsWith('__'));
13
14
  const newDoc = {};
@@ -55,6 +56,7 @@ function getArrayNode(schema, value) {
55
56
  return null;
56
57
  }
57
58
  const clean = async function (info) {
59
+ (0, convertTypedModel_1.convertTypedModel)(info);
58
60
  let { schema, args = [], value } = info;
59
61
  const currSchema = schema.type === undefined ? { type: schema } : schema;
60
62
  const objectSchema = (0, getObjectNode_1.default)(currSchema, value);
@@ -0,0 +1,2 @@
1
+ import { Schema } from '.';
2
+ export declare const getSchemaFromTypedModel: (schema: Schema | Function) => Schema;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSchemaFromTypedModel = void 0;
4
+ const convertTypedModel_1 = require("./getValidationErrors/convertTypedModel");
5
+ const getSchemaFromTypedModel = (schema) => {
6
+ const item = schema;
7
+ if (typeof item !== 'function')
8
+ return item;
9
+ if (!(0, convertTypedModel_1.isClass)(item))
10
+ return item;
11
+ if (!item.getModel || !item.__schemaId)
12
+ return item;
13
+ return item.getModel().getCleanSchema();
14
+ };
15
+ exports.getSchemaFromTypedModel = getSchemaFromTypedModel;
@@ -0,0 +1,3 @@
1
+ import { CurrentNodeInfo } from '../types/schema';
2
+ export declare function isClass(obj: any): boolean;
3
+ export declare const convertTypedModel: (info: CurrentNodeInfo) => void;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertTypedModel = exports.isClass = void 0;
4
+ function isClass(obj) {
5
+ const isCtorClass = obj.constructor && obj.constructor.toString().substring(0, 5) === 'class';
6
+ if (obj.prototype === undefined) {
7
+ return isCtorClass;
8
+ }
9
+ const isPrototypeCtorClass = obj.prototype.constructor &&
10
+ obj.prototype.constructor.toString &&
11
+ obj.prototype.constructor.toString().substring(0, 5) === 'class';
12
+ return isCtorClass || isPrototypeCtorClass;
13
+ }
14
+ exports.isClass = isClass;
15
+ const convertOnParam = (info, paramName) => {
16
+ if (!info[paramName])
17
+ return;
18
+ const type = info[paramName].type;
19
+ if (!type)
20
+ return;
21
+ if (typeof type !== 'function')
22
+ return;
23
+ if (!isClass(type))
24
+ return;
25
+ if (!type.getModel || !type.__schemaId)
26
+ return;
27
+ info[paramName].type = type.getModel().getCleanSchema();
28
+ };
29
+ const convertTypedModel = (info) => {
30
+ convertOnParam(info, 'schema');
31
+ convertOnParam(info, 'currentSchema');
32
+ };
33
+ exports.convertTypedModel = convertTypedModel;
@@ -10,7 +10,9 @@ const clone_1 = __importDefault(require("lodash/clone"));
10
10
  const isNil_1 = __importDefault(require("lodash/isNil"));
11
11
  const difference_1 = __importDefault(require("lodash/difference"));
12
12
  const Errors_1 = __importDefault(require("../Errors"));
13
+ const convertTypedModel_1 = require("./convertTypedModel");
13
14
  async function doValidation(params) {
15
+ (0, convertTypedModel_1.convertTypedModel)(params);
14
16
  const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
15
17
  const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
16
18
  const error = await (0, getError_1.default)(info);
@@ -1,2 +1,2 @@
1
1
  import { Schema } from '../types/schema';
2
- export default function getValidationErrors(schema: Schema, doc: any, passedOptions?: {}, ...args: any[]): Promise<any>;
2
+ export default function getValidationErrors(schema: Schema | Function, doc: any, passedOptions?: {}, ...args: any[]): Promise<any>;
@@ -3,12 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getSchemaFromTypedModel_1 = require("../getSchemaFromTypedModel");
6
7
  const doValidation_1 = __importDefault(require("./doValidation"));
7
8
  const getValidationErrorsObject_1 = __importDefault(require("./getValidationErrorsObject"));
8
9
  const defaultOptions = {
9
10
  omitRequired: false
10
11
  };
11
12
  async function getValidationErrors(schema, doc, passedOptions = {}, ...args) {
13
+ schema = (0, getSchemaFromTypedModel_1.getSchemaFromTypedModel)(schema);
12
14
  const options = { ...defaultOptions, ...passedOptions };
13
15
  const errors = [];
14
16
  const addError = function (keys, code) {
@@ -1,6 +1,7 @@
1
1
  import { FieldType } from '../fieldType';
2
2
  export declare type Constructor<T> = new (...args: any[]) => T;
3
3
  export declare type FieldTypesList = 'string' | 'date' | 'integer' | 'number' | 'ID' | 'boolean' | 'email' | 'blackbox';
4
+ export declare type TypedModelOnSchema = Function;
4
5
  export declare type ConstructorsTypesList = Constructor<String> | Constructor<Number> | Constructor<Boolean> | Constructor<Date>;
5
6
  export declare type SchemaRecursiveNodeTypeExtras = {
6
7
  __clean?: CleanFunction;
@@ -11,8 +12,8 @@ export interface Schema {
11
12
  [key: string]: SchemaNode | Function;
12
13
  }
13
14
  export declare type SchemaRecursiveNodeType = Schema & SchemaRecursiveNodeTypeExtras;
14
- export declare type SchemaMetaFieldTypeSingle = FieldTypesList | ConstructorsTypesList | SchemaRecursiveNodeType | FieldType;
15
- export declare type SchemaMetaFieldType = SchemaMetaFieldTypeSingle | [SchemaMetaFieldTypeSingle];
15
+ export declare type SchemaMetaFieldTypeSingle = FieldTypesList | ConstructorsTypesList | SchemaRecursiveNodeType | FieldType | TypedModelOnSchema;
16
+ export declare type SchemaMetaFieldType = SchemaMetaFieldTypeSingle | SchemaMetaFieldTypeSingle[];
16
17
  export declare type ValidateFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => object | string | void | Promise<object | string | void>;
17
18
  export declare type CleanFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => any | Promise<any>;
18
19
  export interface SchemaNode {
@@ -67,6 +68,14 @@ export interface SchemaNode {
67
68
  * The name that would be displayed in a front-end form
68
69
  */
69
70
  label?: string;
71
+ /**
72
+ * The description that would be displayed in a front-end form
73
+ */
74
+ description?: string;
75
+ /**
76
+ * The placeholder that would be displayed in a front-end form
77
+ */
78
+ placeholder?: string;
70
79
  /**
71
80
  * The field type that would be used in a front-end form
72
81
  */
package/lib/validate.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import { Schema } from './types/schema';
2
- export default function validate(schema: Schema, doc: any, passedOptions?: {}, ...args: any[]): Promise<void>;
2
+ export default function validate(schema: Schema | Function, doc: any, passedOptions?: {}, ...args: any[]): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/schema",
3
- "version": "3.0.17",
3
+ "version": "3.0.37",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -36,5 +36,5 @@
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "55e56828a55b355ec68f91c11d942676bbeb241b"
39
+ "gitHead": "741691d7603f3d42e6e3e8fa9dc0f9de51a29424"
40
40
  }