Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ v1 removes several heavy dependencies:
+ Field,
+ FieldType,
+ FormErrors,
+ ValidationOptions,
+ LegacyOptions,
+ ValidationResult
+ } from '@remoteoss/json-schema-form'
```
Expand All @@ -56,7 +56,7 @@ v1 removes several heavy dependencies:

+ createHeadlessForm(schema, {
+ initialValues: { ... },
+ validationOptions: { ... },
+ legacyOptions: { ... },
+ strictInputType: boolean
+ })
```
Expand Down Expand Up @@ -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`,
Expand Down
16 changes: 8 additions & 8 deletions src/form.ts
Original file line number Diff line number Diff line change
@@ -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[]
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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 })
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
10 changes: 5 additions & 5 deletions src/mutations.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -81,7 +81,7 @@ function evaluateConditional(
function applySchemaRules(
schema: JsfObjectSchema,
values: SchemaValue = {},
options: ValidationOptions = {},
options: LegacyOptions = {},
jsonLogicContext: JsonLogicContext | undefined,
) {
if (!isObjectValue(values)) {
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/validation/array.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand All @@ -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[] {
Expand Down Expand Up @@ -82,7 +82,7 @@ function validateLength(
function validateItems(
schema: NonBooleanJsfSchema,
values: SchemaValue[],
options: ValidationOptions,
options: LegacyOptions,
jsonLogicContext: JsonLogicContext | undefined,
path: ValidationErrorPath,
): ValidationError[] {
Expand Down Expand Up @@ -123,7 +123,7 @@ function validateItems(
function validatePrefixItems(
schema: NonBooleanJsfSchema,
values: SchemaValue[],
options: ValidationOptions,
options: LegacyOptions,
jsonLogicContext: JsonLogicContext | undefined,
path: ValidationErrorPath,
): ValidationError[] {
Expand Down Expand Up @@ -164,7 +164,7 @@ function validatePrefixItems(
function validateContains(
value: SchemaValue[],
schema: NonBooleanJsfSchema,
options: ValidationOptions,
options: LegacyOptions,
jsonLogicContext: JsonLogicContext | undefined,
path: ValidationErrorPath,
): ValidationError[] {
Expand Down
10 changes: 5 additions & 5 deletions src/validation/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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[] {
Expand Down Expand Up @@ -67,7 +67,7 @@ export function validateAllOf(
export function validateAnyOf(
value: SchemaValue,
schema: JsfSchema,
options: ValidationOptions,
options: LegacyOptions,
jsonLogicContext: JsonLogicContext | undefined,
path: ValidationErrorPath = [],
): ValidationError[] {
Expand Down Expand Up @@ -112,7 +112,7 @@ export function validateAnyOf(
export function validateOneOf(
value: SchemaValue,
schema: JsfSchema,
options: ValidationOptions,
options: LegacyOptions,
jsonLogicContext: JsonLogicContext | undefined,
path: ValidationErrorPath = [],
): ValidationError[] {
Expand Down Expand Up @@ -181,7 +181,7 @@ export function validateOneOf(
export function validateNot(
value: SchemaValue,
schema: JsfSchema,
options: ValidationOptions,
options: LegacyOptions,
jsonLogicContext: JsonLogicContext | undefined,
path: ValidationErrorPath = [],
): ValidationError[] {
Expand Down
4 changes: 2 additions & 2 deletions src/validation/conditions.ts
Original file line number Diff line number Diff line change
@@ -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[] {
Expand Down
4 changes: 2 additions & 2 deletions src/validation/custom/date.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/validation/object.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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[] {
Expand Down
6 changes: 3 additions & 3 deletions src/validation/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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 []
}
Expand Down Expand Up @@ -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[] {
Expand Down
2 changes: 1 addition & 1 deletion test/errors/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('createHeadlessForm', () => {
expect(() => {
createHeadlessForm(basicSchema, {
initialValues: { name: 'test' },
validationOptions: {},
legacyOptions: {},
strictInputType: true,
})
}).not.toThrow()
Expand Down