@orion-js/schema 3.2.53 → 3.3.9

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.
@@ -3,11 +3,15 @@ export interface FieldTypeOpts {
3
3
  name: string;
4
4
  validate?: ValidateFunction;
5
5
  clean?: CleanFunction;
6
+ toGraphQLType?: (GraphQL: any) => any;
7
+ meta?: any;
6
8
  }
7
9
  export interface FieldType {
8
10
  name: string;
9
11
  validate: ValidateFunction;
10
12
  clean: CleanFunction;
13
+ meta?: any;
14
+ toGraphQLType?: (GraphQL: any) => any;
11
15
  _isFieldType: boolean;
12
16
  }
13
17
  export default function fieldType(opts: FieldTypeOpts): FieldType;
@@ -0,0 +1,4 @@
1
+ import { FieldType } from '../fieldType';
2
+ export default function createEnum<TValues extends readonly string[]>(name: string, values: TValues): FieldType & {
3
+ type: TValues[number];
4
+ };
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fieldType_1 = __importDefault(require("../fieldType"));
7
+ const isString_1 = __importDefault(require("lodash/isString"));
8
+ const Errors_1 = __importDefault(require("../Errors"));
9
+ const includes_1 = __importDefault(require("lodash/includes"));
10
+ function createEnum(name, values) {
11
+ return {
12
+ type: values[0],
13
+ ...(0, fieldType_1.default)({
14
+ name: 'enum',
15
+ meta: {
16
+ enumName: name,
17
+ enumValues: values
18
+ },
19
+ toGraphQLType: GraphQL => {
20
+ return new GraphQL.GraphQLEnumType({
21
+ name,
22
+ values: values.reduce((result, value) => {
23
+ result[value] = { value };
24
+ return result;
25
+ }, {})
26
+ });
27
+ },
28
+ validate(value, { currentSchema }) {
29
+ if (!(0, isString_1.default)(value))
30
+ return Errors_1.default.NOT_A_STRING;
31
+ if (!(0, includes_1.default)(values, value)) {
32
+ return Errors_1.default.NOT_AN_ALLOWED_VALUE;
33
+ }
34
+ if (value === '' && !currentSchema.optional) {
35
+ return Errors_1.default.REQUIRED;
36
+ }
37
+ },
38
+ clean(value, { options: { autoConvert, trimStrings, removeEmptyStrings } }) {
39
+ if (autoConvert) {
40
+ value = String(value);
41
+ }
42
+ if (trimStrings) {
43
+ value = value.trim();
44
+ }
45
+ if (removeEmptyStrings && value === '') {
46
+ return undefined;
47
+ }
48
+ return value;
49
+ }
50
+ })
51
+ };
52
+ }
53
+ exports.default = createEnum;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const enum_1 = __importDefault(require("./enum"));
7
+ const Errors_1 = __importDefault(require("../Errors"));
8
+ const getValidationErrors_1 = __importDefault(require("../getValidationErrors"));
9
+ test('return an error when the value is incorrect', async () => {
10
+ expect((0, enum_1.default)('hello', ['hello']).validate(['Hello'])).toBe(Errors_1.default.NOT_A_STRING);
11
+ expect((0, enum_1.default)('hello', ['hello']).validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_STRING);
12
+ expect((0, enum_1.default)('hello', ['hello']).validate(new Date())).toBe(Errors_1.default.NOT_A_STRING);
13
+ });
14
+ test('enum typescript helpers', async () => {
15
+ const myEnum = (0, enum_1.default)('HelloEnum', ['hello', 'hi']);
16
+ const string = 'hello';
17
+ });
18
+ test('return no error when the value is correct', async () => {
19
+ const info = { currentSchema: { optional: true } };
20
+ expect((0, enum_1.default)('hello', ['hello']).validate('', info)).toBe(Errors_1.default.NOT_AN_ALLOWED_VALUE);
21
+ expect((0, enum_1.default)('hello', ['hello']).validate('hello')).toBeFalsy();
22
+ });
23
+ test('validate correctly allowed values', async () => {
24
+ expect((0, enum_1.default)('hello', ['hello']).validate('hello')).toBeFalsy();
25
+ expect((0, enum_1.default)('hello', ['hello']).validate('hi')).toBe(Errors_1.default.NOT_AN_ALLOWED_VALUE);
26
+ });
27
+ test('Full schema with enum', async () => {
28
+ const schema = {
29
+ color: {
30
+ type: (0, enum_1.default)('colors', ['red', 'blue', 'green'])
31
+ }
32
+ };
33
+ expect(await (0, getValidationErrors_1.default)(schema, {
34
+ color: 'red'
35
+ })).toBeNull();
36
+ expect(await (0, getValidationErrors_1.default)(schema, {
37
+ color: 'yellow'
38
+ })).toEqual({
39
+ color: Errors_1.default.NOT_AN_ALLOWED_VALUE
40
+ });
41
+ });
@@ -28,6 +28,9 @@ async function doValidation(params) {
28
28
  if ((0, isPlainObject_1.default)(currentSchema.type)) {
29
29
  const type = currentSchema.type;
30
30
  if (type) {
31
+ if (type._isFieldType) {
32
+ return;
33
+ }
31
34
  if (typeof type.__skipChildValidation === 'function') {
32
35
  if (await type.__skipChildValidation(value, info)) {
33
36
  return;
package/lib/index.d.ts CHANGED
@@ -8,5 +8,6 @@ import clean from './clean';
8
8
  import cleanKey from './cleanKey';
9
9
  import validateKey from './validateKey';
10
10
  import dotGetSchema from './dotGetSchema';
11
- export { validate, ValidationError, getValidationErrors, isValid, getFieldType, fieldType, clean, cleanKey, dotGetSchema, validateKey };
11
+ import createEnum from './fieldTypes/enum';
12
+ export { validate, ValidationError, getValidationErrors, isValid, getFieldType, fieldType, clean, cleanKey, dotGetSchema, validateKey, createEnum };
12
13
  export * from './types';
package/lib/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -13,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
18
  };
15
19
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.validateKey = exports.dotGetSchema = exports.cleanKey = exports.clean = exports.fieldType = exports.getFieldType = exports.isValid = exports.getValidationErrors = exports.ValidationError = exports.validate = void 0;
20
+ exports.createEnum = exports.validateKey = exports.dotGetSchema = exports.cleanKey = exports.clean = exports.fieldType = exports.getFieldType = exports.isValid = exports.getValidationErrors = exports.ValidationError = exports.validate = void 0;
17
21
  const validate_1 = __importDefault(require("./validate"));
18
22
  exports.validate = validate_1.default;
19
23
  const ValidationError_1 = __importDefault(require("./ValidationError"));
@@ -34,4 +38,6 @@ const validateKey_1 = __importDefault(require("./validateKey"));
34
38
  exports.validateKey = validateKey_1.default;
35
39
  const dotGetSchema_1 = __importDefault(require("./dotGetSchema"));
36
40
  exports.dotGetSchema = dotGetSchema_1.default;
41
+ const enum_1 = __importDefault(require("./fieldTypes/enum"));
42
+ exports.createEnum = enum_1.default;
37
43
  __exportStar(require("./types"), exports);
@@ -1,2 +1,2 @@
1
1
  import fieldTypes from '../fieldTypes';
2
- export declare type FieldValidatorType = keyof typeof fieldTypes | 'custom' | 'plainObject';
2
+ export type FieldValidatorType = keyof typeof fieldTypes | 'custom' | 'plainObject';
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1,12 +1,13 @@
1
1
  import { FieldType } from '../fieldType';
2
- export declare type Constructor<T> = new (...args: any[]) => T;
3
- export declare type Blackbox = {
2
+ export type Constructor<T> = new (...args: any[]) => T;
3
+ export type Blackbox = {
4
4
  [name: string]: any;
5
5
  };
6
- export declare type FieldTypesList = 'string' | 'date' | 'integer' | 'number' | 'ID' | 'boolean' | 'email' | 'blackbox';
7
- export declare type TypedModelOnSchema = Function;
8
- export declare type ConstructorsTypesList = Constructor<String> | Constructor<Number> | Constructor<Boolean> | Constructor<Date>;
9
- export declare type SchemaRecursiveNodeTypeExtras = {
6
+ export type FieldTypesList = 'string' | 'date' | 'integer' | 'number' | 'ID' | 'boolean' | 'email' | 'blackbox';
7
+ export type TypedModelOnSchema = Function;
8
+ export type ConstructorsTypesList = Constructor<String> | Constructor<Number> | Constructor<Boolean> | Constructor<Date>;
9
+ export type SchemaRecursiveNodeTypeExtras = {
10
+ _isFieldType?: boolean;
10
11
  __clean?: CleanFunction;
11
12
  __validate?: ValidateFunction;
12
13
  __skipChildValidation?: (value: any, info: CurrentNodeInfo) => Promise<boolean>;
@@ -14,11 +15,11 @@ export declare type SchemaRecursiveNodeTypeExtras = {
14
15
  export interface Schema {
15
16
  [key: string]: SchemaNode | Function;
16
17
  }
17
- export declare type SchemaRecursiveNodeType = Schema & SchemaRecursiveNodeTypeExtras;
18
- export declare type SchemaMetaFieldTypeSingle = FieldTypesList | ConstructorsTypesList | SchemaRecursiveNodeType | FieldType | TypedModelOnSchema;
19
- export declare type SchemaMetaFieldType = SchemaMetaFieldTypeSingle | SchemaMetaFieldTypeSingle[];
20
- export declare type ValidateFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => object | string | void | Promise<object | string | void>;
21
- export declare type CleanFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => any | Promise<any>;
18
+ export type SchemaRecursiveNodeType = Schema & SchemaRecursiveNodeTypeExtras;
19
+ export type SchemaMetaFieldTypeSingle = FieldTypesList | ConstructorsTypesList | SchemaRecursiveNodeType | FieldType | TypedModelOnSchema;
20
+ export type SchemaMetaFieldType = SchemaMetaFieldTypeSingle | SchemaMetaFieldTypeSingle[];
21
+ export type ValidateFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => object | string | void | Promise<object | string | void>;
22
+ export type CleanFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => any | Promise<any>;
22
23
  export interface SchemaNode {
23
24
  /**
24
25
  * The type of the field. Used for type validations. Can also contain a subschema.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/schema",
3
- "version": "3.2.53",
3
+ "version": "3.3.9",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -21,20 +21,15 @@
21
21
  "lodash": "^4.17.10"
22
22
  },
23
23
  "devDependencies": {
24
- "@types/eslint": "^7.29.0",
25
- "@types/jest": "27.0.2",
26
- "@types/lodash": "4.14.176",
27
- "@types/node": "16.11.7",
28
- "@typescript-eslint/eslint-plugin": "5.3.1",
29
- "@typescript-eslint/parser": "5.3.1",
30
- "eslint": "8.2.0",
31
- "eslint-plugin-import": "2.25.3",
24
+ "@types/dot-object": "^2.1.2",
25
+ "@types/jest": "^27.4.1",
26
+ "@types/lodash": "4.14.181",
32
27
  "jest": "27.3.1",
33
28
  "ts-jest": "27.0.7",
34
- "typescript": "^4.4.4"
29
+ "typescript": "^4.6.3"
35
30
  },
36
31
  "publishConfig": {
37
32
  "access": "public"
38
33
  },
39
- "gitHead": "4fea736e98ac00e92f0d0f0ff9ba26ffbc7ff6c7"
34
+ "gitHead": "c9591d61fe15eb98c6b6d5c3773afab11c412e42"
40
35
  }