@orion-js/schema 4.3.0 → 4.4.0

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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/dist/Errors.d.ts +21 -0
  3. package/dist/ValidationError.d.ts +16 -0
  4. package/dist/clean/cleanType.d.ts +3 -0
  5. package/dist/clean/getObjectNode.d.ts +2 -0
  6. package/dist/clean/index.d.ts +3 -0
  7. package/dist/clean/recursiveClean.d.ts +3 -0
  8. package/dist/cleanAndValidate.d.ts +2 -0
  9. package/dist/cleanKey.d.ts +1 -0
  10. package/dist/clone.d.ts +1 -0
  11. package/dist/cloneSchema.d.ts +35 -0
  12. package/dist/dotGetSchema.d.ts +2 -0
  13. package/dist/fieldType.d.ts +19 -0
  14. package/dist/fieldTypes/ID.d.ts +2 -0
  15. package/dist/fieldTypes/any.d.ts +2 -0
  16. package/dist/fieldTypes/array.d.ts +2 -0
  17. package/dist/fieldTypes/blackbox.d.ts +3 -0
  18. package/dist/fieldTypes/boolean.d.ts +2 -0
  19. package/dist/fieldTypes/date.d.ts +2 -0
  20. package/dist/fieldTypes/email.d.ts +2 -0
  21. package/dist/fieldTypes/enum.d.ts +2 -0
  22. package/dist/fieldTypes/index.d.ts +14 -0
  23. package/dist/fieldTypes/integer.d.ts +2 -0
  24. package/dist/fieldTypes/number.d.ts +2 -0
  25. package/dist/fieldTypes/plainObject.d.ts +3 -0
  26. package/dist/fieldTypes/string.d.ts +2 -0
  27. package/dist/getValidationErrors/convertTypedSchema.d.ts +2 -0
  28. package/dist/getValidationErrors/doValidation.d.ts +2 -0
  29. package/dist/getValidationErrors/getError/getFieldType.d.ts +4 -0
  30. package/dist/getValidationErrors/getError/getFieldValidator.d.ts +3 -0
  31. package/dist/getValidationErrors/getError/index.d.ts +2 -0
  32. package/dist/getValidationErrors/getFieldLabels.d.ts +6 -0
  33. package/dist/getValidationErrors/getValidationErrorsObject.d.ts +4 -0
  34. package/dist/getValidationErrors/index.d.ts +3 -0
  35. package/dist/index.cjs +2 -1
  36. package/dist/index.cjs.map +1 -1
  37. package/dist/index.d.ts +18 -304
  38. package/dist/index.js +2 -1
  39. package/dist/index.js.map +1 -1
  40. package/dist/isValid.d.ts +3 -0
  41. package/dist/models.d.ts +7 -0
  42. package/dist/schemaWithName/index.d.ts +15 -0
  43. package/dist/types/fieldValidators.d.ts +2 -0
  44. package/dist/types/fields.d.ts +37 -0
  45. package/dist/types/helpers.d.ts +2 -0
  46. package/dist/types/index.d.ts +4 -0
  47. package/dist/types/schema.d.ts +145 -0
  48. package/dist/validate.d.ts +3 -0
  49. package/dist/validateKey/dotGetSchema.d.ts +2 -0
  50. package/dist/validateKey/index.d.ts +2 -0
  51. package/package.json +10 -11
  52. package/dist/index.d.cts +0 -304
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Orionjs Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ declare const _default: {
2
+ NOT_IN_SCHEMA: string;
3
+ REQUIRED: string;
4
+ UNKNOWN_FIELD_TYPE: string;
5
+ NOT_AN_ARRAY: string;
6
+ NOT_AN_OBJECT: string;
7
+ NOT_A_STRING: string;
8
+ NOT_A_DATE: string;
9
+ NOT_AN_INTEGER: string;
10
+ NOT_A_NUMBER: string;
11
+ NOT_AN_ID: string;
12
+ STRING_TOO_SHORT: string;
13
+ STRING_TOO_LONG: string;
14
+ NUMBER_TOO_SMALL: string;
15
+ NUMBER_TOO_BIG: string;
16
+ NOT_A_BOOLEAN: string;
17
+ NOT_AN_EMAIL: string;
18
+ NOT_UNIQUE: string;
19
+ NOT_AN_ALLOWED_VALUE: string;
20
+ };
21
+ export default _default;
@@ -0,0 +1,16 @@
1
+ export interface ValidationErrorInfo {
2
+ error: string;
3
+ message: string;
4
+ validationErrors: Record<string, string>;
5
+ labels: Record<string, string>;
6
+ }
7
+ export default class ValidationError extends Error {
8
+ code: string;
9
+ isValidationError: boolean;
10
+ isOrionError: boolean;
11
+ validationErrors: Record<string, string>;
12
+ labels: Record<string, string>;
13
+ constructor(validationErrors: Record<string, string>, labels?: Record<string, string>);
14
+ getInfo: () => ValidationErrorInfo;
15
+ prependKey: (prepend: any) => ValidationError;
16
+ }
@@ -0,0 +1,3 @@
1
+ import { FieldValidatorType } from '../types/fieldValidators';
2
+ import { CurrentNodeInfo, SchemaFieldType, SchemaNode } from '../types/schema';
3
+ export default function cleanType(type: SchemaFieldType | FieldValidatorType, fieldSchema: Partial<SchemaNode>, value: any, info: CurrentNodeInfo, ...args: any[]): Promise<any>;
@@ -0,0 +1,2 @@
1
+ import { SchemaNode } from '../types/schema';
2
+ export default function getObjectNode(schema: Partial<SchemaNode>, value: any): SchemaNode;
@@ -0,0 +1,3 @@
1
+ import { InferSchemaType } from '../types';
2
+ import { CurrentNodeInfoOptions, SchemaFieldType } from '../types/schema';
3
+ export default function clean<TSchema extends SchemaFieldType>(schema: TSchema, doc: InferSchemaType<TSchema>, opts?: CurrentNodeInfoOptions, ...args: any[]): Promise<InferSchemaType<TSchema>>;
@@ -0,0 +1,3 @@
1
+ import { CurrentNodeInfo } from '../types/schema';
2
+ declare const clean: (info: CurrentNodeInfo) => Promise<any>;
3
+ export default clean;
@@ -0,0 +1,2 @@
1
+ import { InferSchemaType, SchemaFieldType } from './types';
2
+ export declare function cleanAndValidate<TSchema extends SchemaFieldType>(schema: TSchema, doc: InferSchemaType<TSchema>): Promise<InferSchemaType<TSchema>>;
@@ -0,0 +1 @@
1
+ export default function (schema: any, key: any, value: any, passedOptions?: {}, ...args: any[]): Promise<any>;
@@ -0,0 +1 @@
1
+ export declare function clone<T>(value: T): T;
@@ -0,0 +1,35 @@
1
+ import { Schema, SchemaNode } from './types';
2
+ export type CloneSchemaOptions<TSchema extends Schema, TExtendFields extends Schema | undefined = undefined, TPickFields extends (keyof TSchema)[] | undefined = undefined, TOmitFields extends (keyof TSchema)[] | undefined = undefined> = {
3
+ /**
4
+ * The schema to clone
5
+ */
6
+ schema: TSchema;
7
+ /**
8
+ * The name of the cloned schema
9
+ */
10
+ name?: string;
11
+ /**
12
+ * The schema to extend the cloned schema with
13
+ */
14
+ extendSchema?: TExtendFields;
15
+ /**
16
+ * A function to map the fields of the cloned schema.
17
+ * Warning: This function will not be applied to the typescript types of this schema.
18
+ */
19
+ mapFields?: (field: TSchema[keyof TSchema], key: keyof TSchema) => SchemaNode;
20
+ /**
21
+ * The fields to pick from the cloned schema
22
+ */
23
+ pickFields?: TPickFields;
24
+ /**
25
+ * The fields to omit from the cloned schema
26
+ */
27
+ omitFields?: TOmitFields;
28
+ };
29
+ type ExtendFields<TSchema extends Schema, TExtendFields extends Schema | undefined> = TExtendFields extends undefined ? TSchema : {
30
+ [key in keyof (TSchema & TExtendFields)]: key extends keyof TExtendFields ? TExtendFields[key] : key extends keyof TSchema ? TSchema[key] : never;
31
+ };
32
+ type PickOrOmit<TSchema extends Schema, TPickFields extends (keyof TSchema)[] | undefined = undefined, TOmitFields extends (keyof TSchema)[] | undefined = undefined> = TPickFields extends undefined ? TOmitFields extends undefined ? TSchema : Omit<TSchema, TOmitFields[number]> : Pick<TSchema, TPickFields[number]>;
33
+ export type ClonedSchema<TSchema extends Schema, TExtendFields extends Schema | undefined = undefined, TPickFields extends (keyof TSchema)[] | undefined = undefined, TOmitFields extends (keyof TSchema)[] | undefined = undefined> = ExtendFields<PickOrOmit<TSchema, TPickFields, TOmitFields>, TExtendFields>;
34
+ export declare function cloneSchema<TSchema extends Schema, TExtendFields extends Schema | undefined = undefined, TPickFields extends (keyof TSchema)[] | undefined = undefined, TOmitFields extends (keyof TSchema)[] | undefined = undefined>(options: CloneSchemaOptions<TSchema, TExtendFields, TPickFields, TOmitFields>): ClonedSchema<TSchema, TExtendFields, TPickFields, TOmitFields>;
35
+ export {};
@@ -0,0 +1,2 @@
1
+ import dotGetSchema from './validateKey/dotGetSchema';
2
+ export default dotGetSchema;
@@ -0,0 +1,19 @@
1
+ import { CleanFunction, SchemaFieldType, SchemaNode, ValidateFunction } from './types/schema';
2
+ export interface FieldTypeOpts<TType = any> {
3
+ name: string;
4
+ validate?: ValidateFunction<TType>;
5
+ clean?: CleanFunction<TType>;
6
+ toGraphQLType?: (GraphQL: any) => any;
7
+ meta?: any;
8
+ }
9
+ export interface FieldType<TType = any> {
10
+ name: string;
11
+ validate: ValidateFunction;
12
+ clean: CleanFunction;
13
+ meta?: any;
14
+ toGraphQLType?: (GraphQL: any) => any;
15
+ toSerializedType?: (node: SchemaNode) => Promise<SchemaFieldType>;
16
+ __tsFieldType: TType;
17
+ __isFieldType: boolean;
18
+ }
19
+ export default function fieldType<TType>(opts: FieldTypeOpts<TType>): FieldType<TType>;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<string>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<any>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<any[]>;
2
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import { Blackbox } from '../types';
2
+ declare const _default: import("..").FieldType<Blackbox>;
3
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<boolean>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<Date>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<string>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ import { FieldType } from '../fieldType';
2
+ export default function createEnum<const TValues extends readonly string[]>(name: string, values: TValues): FieldType<TValues[number]>;
@@ -0,0 +1,14 @@
1
+ declare const _default: {
2
+ array: import("..").FieldType<any[]>;
3
+ plainObject: import("..").FieldType<import("..").Blackbox>;
4
+ string: import("..").FieldType<string>;
5
+ date: import("..").FieldType<Date>;
6
+ integer: import("..").FieldType<number>;
7
+ number: import("..").FieldType<number>;
8
+ ID: import("..").FieldType<string>;
9
+ boolean: import("..").FieldType<boolean>;
10
+ email: import("..").FieldType<string>;
11
+ blackbox: import("..").FieldType<import("..").Blackbox>;
12
+ any: import("..").FieldType<any>;
13
+ };
14
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<number>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<number>;
2
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import { Blackbox } from '../types';
2
+ declare const _default: import("..").FieldType<Blackbox>;
3
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("..").FieldType<string>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ import { CurrentNodeInfo } from '../types/schema';
2
+ export declare const convertTypedSchema: (info: CurrentNodeInfo) => void;
@@ -0,0 +1,2 @@
1
+ import { CurrentNodeInfo } from '../types/schema';
2
+ export default function doValidation(params: CurrentNodeInfo): Promise<void>;
@@ -0,0 +1,4 @@
1
+ import { SchemaFieldType } from '../../types/schema';
2
+ import { FieldValidatorType } from '../../types/fieldValidators';
3
+ import { FieldType } from '../../fieldType';
4
+ export default function getFieldType(type: SchemaFieldType | FieldValidatorType | any): FieldType;
@@ -0,0 +1,3 @@
1
+ import { FieldValidatorType } from '../../types/fieldValidators';
2
+ import { SchemaFieldType } from '../../types';
3
+ export default function getFieldValidator(type: SchemaFieldType): FieldValidatorType;
@@ -0,0 +1,2 @@
1
+ import { CurrentNodeInfo } from '../../types/schema';
2
+ export default function getValidationErrors(params: CurrentNodeInfo): Promise<object | string | void>;
@@ -0,0 +1,6 @@
1
+ import { SchemaFieldType } from '../types/schema';
2
+ /**
3
+ * Extracts field labels from a schema, creating a flat object with dot-notation keys
4
+ * matching the validation error keys and their corresponding labels.
5
+ */
6
+ export default function getFieldLabels(schema: SchemaFieldType, currentKey?: string): Record<string, string>;
@@ -0,0 +1,4 @@
1
+ export default function getValidationErrorsObject(validationErrors: {
2
+ key: string;
3
+ code: string;
4
+ }[]): any;
@@ -0,0 +1,3 @@
1
+ import { InferSchemaType } from '../types';
2
+ import { SchemaFieldType } from '../types/schema';
3
+ export default function getValidationErrors<TSchema extends SchemaFieldType>(schema: TSchema, doc: InferSchemaType<TSchema>, passedOptions?: {}, ...args: any[]): Promise<any>;
package/dist/index.cjs CHANGED
@@ -915,7 +915,8 @@ function getArrayNode(schema2, value) {
915
915
  var clean = async (info) => {
916
916
  convertTypedSchema(info);
917
917
  const { schema: schema2, args = [], value } = info;
918
- const currSchema = schema2.type === void 0 ? { type: schema2 } : schema2;
918
+ const schemaValue = schema2;
919
+ const currSchema = typeof schemaValue === "object" && schemaValue !== null && "type" in schemaValue ? schemaValue : { type: schemaValue };
919
920
  const objectSchema = getObjectNode(currSchema, value);
920
921
  if (objectSchema) {
921
922
  const newDoc = await cleanObjectFields({