diff --git a/MIGRATING.md b/MIGRATING.md index b8112ecb1..650006e31 100644 --- a/MIGRATING.md +++ b/MIGRATING.md @@ -40,7 +40,7 @@ v1 removes several heavy dependencies: + Field, + FieldType, + FormErrors, -+ ValidationOptions, ++ LegacyOptions, + ValidationResult + } from '@remoteoss/json-schema-form' ``` @@ -56,7 +56,7 @@ v1 removes several heavy dependencies: + createHeadlessForm(schema, { + initialValues: { ... }, -+ validationOptions: { ... }, ++ legacyOptions: { ... }, + strictInputType: boolean + }) ``` @@ -252,11 +252,11 @@ If you get import errors, ensure your project supports ESM: ### 2. **Validation backward compatibility with v0** Some validation behaviors *were wrong* in `v0`, we fixed them in v1. -If you still need them, you need to enable them in the `validationOptions` config. +If you still need them, you need to enable them in the `legacyOptions` config. ```typescript const form = createHeadlessForm(schema, { - validationOptions: { + legacyOptions: { /** * A null value will be treated as undefined. * When true, providing a value to a schema that is `false`, diff --git a/src/form.ts b/src/form.ts index 2a2c586e7..2a5308511 100644 --- a/src/form.ts +++ b/src/form.ts @@ -1,13 +1,13 @@ import type { ValidationError, ValidationErrorPath } from './errors' import type { Field } from './field/type' import type { JsfObjectSchema, JsfSchema, SchemaValue } from './types' -import type { ValidationOptions } from './validation/schema' +import type { LegacyOptions } from './validation/schema' import { getErrorMessage } from './errors/messages' import { buildFieldSchema } from './field/schema' import { calculateFinalSchema, updateFieldProperties } from './mutations' import { validateSchema } from './validation/schema' -export { ValidationOptions } from './validation/schema' +export { LegacyOptions } from './validation/schema' interface FormResult { fields: Field[] @@ -198,7 +198,7 @@ function applyCustomErrorMessages(errors: ValidationErrorWithMessage[], schema: * @param schema - The schema to validate against * @returns The validation result */ -function validate(value: SchemaValue, schema: JsfSchema, options: ValidationOptions = {}): ValidationResult { +function validate(value: SchemaValue, schema: JsfSchema, options: LegacyOptions = {}): ValidationResult { const result: ValidationResult = {} const errors = validateSchema(value, schema, options) @@ -220,9 +220,9 @@ export interface CreateHeadlessFormOptions { */ initialValues?: SchemaValue /** - * The validation options to use for the form + * Backward compatibility config with v0 */ - validationOptions?: ValidationOptions + legacyOptions?: LegacyOptions /** * When enabled, ['x-jsf-presentation'].inputType is required for all properties. * @default false @@ -264,7 +264,7 @@ export function createHeadlessForm( const updatedSchema = calculateFinalSchema({ schema, values: initialValues, - options: options.validationOptions, + options: options.legacyOptions, }) const fields = buildFields({ schema: updatedSchema, originalSchema: schema, strictInputType }) @@ -276,10 +276,10 @@ export function createHeadlessForm( const updatedSchema = calculateFinalSchema({ schema, values: value, - options: options.validationOptions, + options: options.legacyOptions, }) - const result = validate(value, updatedSchema, options.validationOptions) + const result = validate(value, updatedSchema, options.legacyOptions) // Fields properties might have changed, so we need to reset the fields by updating them in place updateFieldProperties(fields, updatedSchema, schema) diff --git a/src/index.ts b/src/index.ts index df11b14b7..84686fb5b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ export { createHeadlessForm, type CreateHeadlessFormOptions, type FormErrors, - type ValidationOptions, + type LegacyOptions, type ValidationResult, } from './form' export { modifySchema as modify } from './modify-schema' diff --git a/src/mutations.ts b/src/mutations.ts index be8ab58e3..b0e67a619 100644 --- a/src/mutations.ts +++ b/src/mutations.ts @@ -1,6 +1,6 @@ import type { Field } from './field/type' import type { JsfObjectSchema, JsfSchema, JsonLogicContext, NonBooleanJsfSchema, ObjectValue, SchemaValue } from './types' -import type { ValidationOptions } from './validation/schema' +import type { LegacyOptions } from './validation/schema' import { buildFieldSchema } from './field/schema' import { deepMergeSchemas } from './utils' import { applyComputedAttrsToSchema, getJsonLogicContextFromSchema } from './validation/json-logic' @@ -23,7 +23,7 @@ export function calculateFinalSchema({ }: { schema: JsfObjectSchema values: SchemaValue - options?: ValidationOptions + options?: LegacyOptions }): JsfObjectSchema { const jsonLogicContext = schema['x-jsf-logic'] ? getJsonLogicContextFromSchema(schema['x-jsf-logic'], values) : undefined const schemaCopy = safeDeepClone(schema) @@ -49,7 +49,7 @@ function evaluateConditional( values: ObjectValue, schema: JsfObjectSchema, rule: NonBooleanJsfSchema, - options: ValidationOptions = {}, + options: LegacyOptions = {}, ) { const conditionIsTrue = validateSchema(values, rule.if!, options).length === 0 @@ -81,7 +81,7 @@ function evaluateConditional( function applySchemaRules( schema: JsfObjectSchema, values: SchemaValue = {}, - options: ValidationOptions = {}, + options: LegacyOptions = {}, jsonLogicContext: JsonLogicContext | undefined, ) { if (!isObjectValue(values)) { @@ -142,7 +142,7 @@ function applySchemaRules( * @param options - Validation options * @param jsonLogicContext - JSON Logic context */ -function processBranch(schema: JsfObjectSchema, values: SchemaValue, branch: JsfSchema, options: ValidationOptions = {}, jsonLogicContext: JsonLogicContext | undefined) { +function processBranch(schema: JsfObjectSchema, values: SchemaValue, branch: JsfSchema, options: LegacyOptions = {}, jsonLogicContext: JsonLogicContext | undefined) { const branchSchema = branch as JsfObjectSchema applySchemaRules(branchSchema, values, options, jsonLogicContext) diff --git a/src/validation/array.ts b/src/validation/array.ts index 795e31085..eb14441ea 100644 --- a/src/validation/array.ts +++ b/src/validation/array.ts @@ -1,6 +1,6 @@ import type { ValidationError, ValidationErrorPath } from '../errors' import type { JsfSchema, JsonLogicContext, NonBooleanJsfSchema, SchemaValue } from '../types' -import { validateSchema, type ValidationOptions } from './schema' +import { type LegacyOptions, validateSchema } from './schema' import { deepEqual } from './util' /** @@ -18,7 +18,7 @@ import { deepEqual } from './util' export function validateArray( value: SchemaValue, schema: JsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath, ): ValidationError[] { @@ -82,7 +82,7 @@ function validateLength( function validateItems( schema: NonBooleanJsfSchema, values: SchemaValue[], - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath, ): ValidationError[] { @@ -123,7 +123,7 @@ function validateItems( function validatePrefixItems( schema: NonBooleanJsfSchema, values: SchemaValue[], - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath, ): ValidationError[] { @@ -164,7 +164,7 @@ function validatePrefixItems( function validateContains( value: SchemaValue[], schema: NonBooleanJsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath, ): ValidationError[] { diff --git a/src/validation/composition.ts b/src/validation/composition.ts index a1e91f9ce..32d139954 100644 --- a/src/validation/composition.ts +++ b/src/validation/composition.ts @@ -6,7 +6,7 @@ */ import type { ValidationError, ValidationErrorPath } from '../errors' -import type { ValidationOptions } from '../form' +import type { LegacyOptions } from '../form' import type { JsfSchema, JsonLogicContext, SchemaValue } from '../types' import { validateSchema } from './schema' @@ -29,7 +29,7 @@ import { validateSchema } from './schema' export function validateAllOf( value: SchemaValue, schema: JsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath = [], ): ValidationError[] { @@ -67,7 +67,7 @@ export function validateAllOf( export function validateAnyOf( value: SchemaValue, schema: JsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath = [], ): ValidationError[] { @@ -112,7 +112,7 @@ export function validateAnyOf( export function validateOneOf( value: SchemaValue, schema: JsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath = [], ): ValidationError[] { @@ -181,7 +181,7 @@ export function validateOneOf( export function validateNot( value: SchemaValue, schema: JsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath = [], ): ValidationError[] { diff --git a/src/validation/conditions.ts b/src/validation/conditions.ts index f0bf78801..fbe8dc327 100644 --- a/src/validation/conditions.ts +++ b/src/validation/conditions.ts @@ -1,12 +1,12 @@ import type { ValidationError, ValidationErrorPath } from '../errors' -import type { ValidationOptions } from '../form' +import type { LegacyOptions } from '../form' import type { JsonLogicContext, NonBooleanJsfSchema, SchemaValue } from '../types' import { validateSchema } from './schema' export function validateCondition( value: SchemaValue, schema: NonBooleanJsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath = [], ): ValidationError[] { diff --git a/src/validation/custom/date.ts b/src/validation/custom/date.ts index f81cf1d74..2f8c58bd7 100644 --- a/src/validation/custom/date.ts +++ b/src/validation/custom/date.ts @@ -1,6 +1,6 @@ import type { ValidationError, ValidationErrorPath } from '../../errors' import type { NonBooleanJsfSchema, SchemaValue } from '../../types' -import type { ValidationOptions } from '../schema' +import type { LegacyOptions } from '../schema' export const DATE_FORMAT = 'yyyy-MM-dd' type DateComparisonResult = 'LESSER' | 'GREATER' | 'EQUAL' @@ -62,7 +62,7 @@ function validateMaxDate(value: string, maxDate: string): boolean { export function validateDate( value: SchemaValue, schema: NonBooleanJsfSchema, - options: ValidationOptions, + options: LegacyOptions, path: ValidationErrorPath = [], ): ValidationError[] { const isString = typeof value === 'string' diff --git a/src/validation/object.ts b/src/validation/object.ts index 1188f4271..728b65658 100644 --- a/src/validation/object.ts +++ b/src/validation/object.ts @@ -1,5 +1,5 @@ import type { ValidationError, ValidationErrorPath } from '../errors' -import type { ValidationOptions } from '../form' +import type { LegacyOptions } from '../form' import type { JsonLogicContext, NonBooleanJsfSchema, SchemaValue } from '../types' import { validateSchema } from './schema' import { isObjectValue } from './util' @@ -19,7 +19,7 @@ import { isObjectValue } from './util' export function validateObject( value: SchemaValue, schema: NonBooleanJsfSchema, - options: ValidationOptions, + options: LegacyOptions, jsonLogicContext: JsonLogicContext | undefined, path: ValidationErrorPath = [], ): ValidationError[] { diff --git a/src/validation/schema.ts b/src/validation/schema.ts index f3dfe9697..9030a8f21 100644 --- a/src/validation/schema.ts +++ b/src/validation/schema.ts @@ -13,7 +13,7 @@ import { validateObject } from './object' import { validateString } from './string' import { isObjectValue } from './util' -export interface ValidationOptions { +export interface LegacyOptions { /** * A null value will be treated as undefined. * When true, providing a value to a schema that is `false`, @@ -139,7 +139,7 @@ function validateType( * @param jsonLogicContext - The json-logic context * @returns An array of validation errors */ -function validateJsonLogicSchema(value: SchemaValue, schema: JsfSchema | undefined, options: ValidationOptions = {}, path: ValidationErrorPath = [], jsonLogicContext?: JsonLogicContext): ValidationError[] { +function validateJsonLogicSchema(value: SchemaValue, schema: JsfSchema | undefined, options: LegacyOptions = {}, path: ValidationErrorPath = [], jsonLogicContext?: JsonLogicContext): ValidationError[] { if (!schema) { return [] } @@ -179,7 +179,7 @@ function validateJsonLogicSchema(value: SchemaValue, schema: JsfSchema | undefin export function validateSchema( value: SchemaValue, schema: JsfSchema, - options: ValidationOptions = {}, + options: LegacyOptions = {}, path: ValidationErrorPath = [], rootJsonLogicContext?: JsonLogicContext, ): ValidationError[] { diff --git a/test/errors/messages.test.ts b/test/errors/messages.test.ts index 32ce4177f..f1e2ed3e2 100644 --- a/test/errors/messages.test.ts +++ b/test/errors/messages.test.ts @@ -467,7 +467,7 @@ describe('validation error messages', () => { expect(result.formErrors).toBeUndefined() - form = createHeadlessForm(schema, { validationOptions: { treatNullAsUndefined: true } }) + form = createHeadlessForm(schema, { legacyOptions: { treatNullAsUndefined: true } }) result = form.handleValidation({ dateWithoutConstraints: null, diff --git a/test/form.test.ts b/test/form.test.ts index 78f9dba1c..b0885e270 100644 --- a/test/form.test.ts +++ b/test/form.test.ts @@ -31,7 +31,7 @@ describe('createHeadlessForm', () => { expect(() => { createHeadlessForm(basicSchema, { initialValues: { name: 'test' }, - validationOptions: {}, + legacyOptions: {}, strictInputType: true, }) }).not.toThrow()