UNPKG

@palmares/schemas

Version:

This defines a default schema definition for validation of data, it abstract popular schema validation libraries like zod, yup, valibot and others"

94 lines 6.9 kB
import type { SchemaAdapter } from './adapter'; import type { FieldAdapter } from './adapter/fields'; import type { ValidationDataBasedOnType } from './adapter/types'; import type { Schema } from './schema/schema'; import type { ValidationFallbackCallbackReturnType, ValidationFallbackReturnType } from './schema/types'; import type { FallbackFunctionsType, SupportedSchemas } from './types'; /** * The usage of this is that imagine that the library doesn't support a specific feature that we support on * our schema definition, it can return an instance of this class and with this instance we are able to * fallback to our default implementation of the schema validation. */ export declare class WithFallback<TType extends SupportedSchemas> { protected $$type: string; fallbackFor: Set<keyof Omit<ValidationDataBasedOnType<TType>, 'withFallback' | 'parsers'> | keyof ValidationDataBasedOnType<TType>['parsers']>; transformedSchema: any; adapterType: TType; constructor(adapterType: TType, fallbackFor: (keyof Omit<ValidationDataBasedOnType<TType>, 'withFallback' | 'parsers'> | keyof ValidationDataBasedOnType<TType>['parsers'])[], transformedSchema: any); } /** * Factory function for creating a new instance of WithFallback. We call that function when parsing the * schema adapter, and then, inside of the adapter the user will can the inner function to create a new * instance of WithFallback. * * @param adapterType - The type of the adapter that we are using. * * @returns - A currying function that will create a new instance of WithFallback. */ export declare function withFallbackFactory<TType extends SupportedSchemas>(adapterType: TType): (fallbackFor: (keyof Omit<ValidationDataBasedOnType<TType>, "withFallback" | "parsers"> | keyof ValidationDataBasedOnType<TType>["parsers"])[], transformedSchema: WithFallback<SupportedSchemas>["transformedSchema"]) => WithFallback<TType>; export declare function parseErrorsFactory(schemaAdapter: SchemaAdapter): (errorOrErrors: any | any[], metadata?: any) => Promise<Awaited<ReturnType<SchemaAdapter["formatError"]>>[]>; /** * The default transform function that we use for the schema adapters. This function tries to abstract away * the complexity of translating the schema to the adapter. * * So first things first, WHAT IS a fallback? A fallback is a function that we call when the user defines a * validation that is not supported by the adapter. For example, imagine that for some reason the adapter * doesn't support the `max` validation, we can define a fallback for that validation and then, when the * user defines that validation, we call the fallback function. So, even if the adapter doesn't support that * validation our schema will still be able to validate that. * * @param type - The type of the adapter that we are using, can be a number, an object, all of the possible * schema adapters. * @param schema - The schema that we are translating. * @param validationData - The data that we are using to validate the schema. This means for example, the * `max` validation, the `min` validation, etc. The message of the validation when it is not valid, etc. * @param fallbackFunctions - The fallback functions that we are using to validate the schema. Those are * the functions we fallback to when the user defines a validation that is not supported by the adapter. * * @returns - The translated schema for something that the adapter is able to understand. */ export declare function defaultTransform<TType extends SupportedSchemas>(type: TType, schema: Schema<any, any>, adapter: SchemaAdapter, fieldAdapter: FieldAdapter | undefined, getValidationData: (isStringVersion: boolean) => ValidationDataBasedOnType<TType>, fallbackFunctions: FallbackFunctionsType<Omit<Awaited<ReturnType<typeof getValidationData>>, 'parsers'>>, options: { validatorsIfFallbackOrNotSupported?: ValidationFallbackReturnType | ValidationFallbackReturnType[]; /** * If the schema is not supported by the adapter, this means, that the adapter hasn't defined an adapter * for that field type, we can fallback to a custom implementation. * The problem is that, for example: Unions, * * Let's say we have a schema like this: * ObjectSchema.new({ age: UnionSchema.new([NumberSchema.new(), StringSchema.new()] )}); * * The root object will be validated by the adapter so what we need to do is create two schemas on the * root object, one where the value of `age` key is a number and another where the value of `age` key is * a string. Now the root object has two schemas memoized on __transformedSchemas, nice, what's the logic * on that case? The ObjectSchema shouldn't take care of that logic. So the Union schema takes control of * validating through the adapter. Is adapter 1 without errors? If yes, return the result, if not, try * the second adapter. If the second adapter is without errors, return the result, if not, return the * errors. * * In other words, the `fallbackIfNotSupported` function on Unions should return the two schemas saved on * it on that case, that way the root ObjectSchema will create those two schemas on the array. */ fallbackIfNotSupported?: () => ReturnType<Schema['__transformToAdapter']>; } & Pick<Parameters<Schema['__transformToAdapter']>[0], 'shouldAddStringVersion'>): Promise<any[]>; /** * This function is used to transform the schema to the adapter. By default it caches the transformed schemas on * the schema instance. So on subsequent validations we don't need to transform to the schema again. */ export declare function defaultTransformToAdapter(callback: (adapter: SchemaAdapter) => ReturnType<FieldAdapter['translate']>, schema: Schema<any, any>, transformedSchemas: Schema['__transformedSchemas'], options: Parameters<Schema['__transformToAdapter']>[0], type: string): Promise<any[]>; export declare function formatErrorFromParseMethod(adapter: SchemaAdapter, fieldAdapter: FieldAdapter, error: any, received: any, schema: any, path: ValidationFallbackCallbackReturnType['errors'][number]['path'], errorsAsHashedSet: Set<string>): Promise<{ received: any; isValid: boolean; code: import("./adapter/types").ErrorCodes; message: string; path: (string | number)[]; }>; /** * Transform the schema and check if we should add a fallback validation for that schema. This is used for complex * schemas like Objects, arrays, unions, etc. */ export declare function transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas(schema: Schema, options: Parameters<Schema['__transformToAdapter']>[0]): Promise<readonly [{ transformed: ReturnType<FieldAdapter["translate"]>; asString: string; }[], boolean]>; export declare function shouldRunDataOnComplexSchemas(schema: Schema<any, any>): boolean; //# sourceMappingURL=utils.d.ts.map