Skip to content

Commit

Permalink
chore(forms): re-export JSONSchema type (#3230)
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Jan 16, 2024
1 parent c3ea6a6 commit 96b574c
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { JSONSchema7 } from 'json-schema'
import ComponentBox from '../../../../../../../shared/tags/ComponentBox'
import {
Form,
DataContext,
Field,
Value,
JSONSchema,
} from '@dnb/eufemia/src/extensions/forms'
import { Card, Flex } from '@dnb/eufemia/src'

export const TestdataSchema: JSONSchema7 = {
export const TestdataSchema: JSONSchema = {
type: 'object',
properties: {
requiredString: { type: 'string' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Since a [DataContext](/uilib/extensions/forms/extended-features/DataContext/) (u
vs.

```tsx
const schema = {
import { JSONSchema } from '@dnb/eufemia/extensions/forms'
const schema: JSONSchema = {
properties: {
name: { minLength: 3 },
email: { type: 'string' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import React, {
useReducer,
} from 'react'
import pointer, { JsonObject } from 'json-pointer'
import { JSONSchema7 } from 'json-schema'
import { ValidateFunction } from 'ajv'
import ajv, { ajvErrorsToFormErrors } from '../../utils/ajv'
import { FormError } from '../../types'
import { FormError, JSONSchema } from '../../types'
import useMountEffect from '../../hooks/useMountEffect'
import useUpdateEffect from '../../hooks/useUpdateEffect'
import { useSharedState } from '../../../../shared/helpers/useSharedState'
Expand All @@ -32,7 +31,7 @@ export interface Props<Data extends JsonObject> {
/** Dynamic source data used as both initial data, and updates internal data if changed after mount */
data?: Partial<Data>
/** JSON Schema for validating the data, like during input or after attempting submit */
schema?: JSONSchema7
schema?: JSONSchema
/** Change handler for the whole data set */
onChange?: (data: Data) => void
/** Change handler for each value */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import {
waitFor,
} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { Form, DataContext, Field } from '../../../'
import { Form, DataContext, Field, JSONSchema } from '../../../'
import { Props as StringFieldProps } from '../../../Field/String/String'
import { JSONSchema7 } from 'json-schema'
import nbNO from '../../../../../shared/locales/nb-NO'

const nb = nbNO['nb-NO'].Forms
Expand Down Expand Up @@ -556,7 +555,7 @@ describe('DataContext.Provider', () => {

describe('schema validation', () => {
it('should handle errors from inner components and outer provider interchangeably', async () => {
const schema: JSONSchema7 = {
const schema: JSONSchema = {
type: 'object',
properties: {
txt: {
Expand Down Expand Up @@ -613,7 +612,7 @@ describe('DataContext.Provider', () => {
})

it('should show provided errorMessages based on outer schema validation with injected value', () => {
const schema: JSONSchema7 = {
const schema: JSONSchema = {
type: 'object',
properties: {
val: {
Expand All @@ -639,7 +638,7 @@ describe('DataContext.Provider', () => {
})

it('should show default errorMessages based on outer schema validation with injected value', () => {
const schema: JSONSchema7 = {
const schema: JSONSchema = {
type: 'object',
properties: {
val: {
Expand All @@ -665,7 +664,7 @@ describe('DataContext.Provider', () => {
it('should call "onSubmitRequest" on invalid submit set by a schema', () => {
const onSubmitRequest = jest.fn()

const TestdataSchema: JSONSchema7 = {
const TestdataSchema: JSONSchema = {
type: 'object',
properties: {
foo: { type: 'number', minimum: 3 },
Expand Down Expand Up @@ -715,7 +714,7 @@ describe('DataContext.Provider', () => {
})

it('should revalidate with provided schema based on changes in external data', () => {
const schema: JSONSchema7 = {
const schema: JSONSchema = {
type: 'object',
properties: {
somekey: {
Expand Down Expand Up @@ -766,15 +765,15 @@ describe('DataContext.Provider', () => {
})

it('should revalidate correctly based on changes in provided schema', () => {
const schema1: JSONSchema7 = {
const schema1: JSONSchema = {
type: 'object',
properties: {
somekey: {
type: 'number',
},
},
}
const schema2: JSONSchema7 = {
const schema2: JSONSchema = {
type: 'object',
properties: {
somekey: {
Expand Down
5 changes: 2 additions & 3 deletions packages/dnb-eufemia/src/extensions/forms/Field/Date/Date.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, { useCallback, useContext, useMemo } from 'react'
import { DatePicker, HelpButton } from '../../../../components'
import { useDataValue } from '../../hooks'
import { FieldProps, FieldHelpProps } from '../../types'
import { FieldProps, FieldHelpProps, JSONSchema } from '../../types'
import { pickSpacingProps } from '../../../../components/flex/utils'
import SharedContext from '../../../../shared/Context'
import { JSONSchema7 } from 'json-schema'
import classnames from 'classnames'
import FieldBlock from '../../FieldBlock'
import { parseISO, isValid } from 'date-fns'
Expand All @@ -28,7 +27,7 @@ function DateComponent(props: Props) {
[tr, props.errorMessages]
)

const schema = useMemo<JSONSchema7>(
const schema = useMemo<JSONSchema>(
() =>
props.schema ?? {
type: 'string',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { useContext, useMemo, useCallback } from 'react'
import { JSONSchema7 } from 'json-schema'
import { InputMasked, HelpButton, Button } from '../../../../components'
import { InputMaskedProps } from '../../../../components/InputMasked'
import type { InputAlign, InputSize } from '../../../../components/Input'
import SharedContext from '../../../../shared/Context'
import classnames from 'classnames'
import FieldBlock from '../../FieldBlock'
import { useDataValue } from '../../hooks'
import { FieldProps, FieldHelpProps } from '../../types'
import { FieldProps, FieldHelpProps, JSONSchema } from '../../types'
import { pickSpacingProps } from '../../../../components/flex/utils'
import { ButtonProps, ButtonSize } from '../../../../components/Button'
import { clamp } from '../../../../components/slider/SliderHelpers'
Expand Down Expand Up @@ -77,7 +76,7 @@ function NumberComponent(props: Props) {
}),
[tr, props.errorMessages]
)
const schema = useMemo<JSONSchema7>(
const schema = useMemo<JSONSchema>(
() =>
props.schema ?? {
type: 'number',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { fireEvent, render } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import PhoneNumber from '..'
import { Provider } from '../../../../../shared'
import { Form } from '../../..'
import { JSONSchema7 } from 'json-schema'
import { Form, JSONSchema } from '../../..'

describe('Field.PhoneNumber', () => {
it('should default to 47', () => {
Expand Down Expand Up @@ -705,7 +704,7 @@ describe('Field.PhoneNumber', () => {
})

it('should validate schema', async () => {
const schema: JSONSchema7 = {
const schema: JSONSchema = {
type: 'string',
pattern: '^\\+47 [49]+',
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useContext, useMemo, useCallback } from 'react'
import classnames from 'classnames'
import { JSONSchema7 } from 'json-schema'
import { HelpButton, Input, Textarea } from '../../../../components'
import { InputProps } from '../../../../components/input/Input'
import InputMasked, {
Expand All @@ -9,7 +8,7 @@ import InputMasked, {
import SharedContext from '../../../../shared/Context'
import FieldBlock from '../../FieldBlock'
import { useDataValue } from '../../hooks'
import { FieldProps, FieldHelpProps } from '../../types'
import { FieldProps, FieldHelpProps, JSONSchema } from '../../types'
import { pickSpacingProps } from '../../../../components/flex/utils'
import { toCapitalized } from '../../../../shared/component-helper'

Expand Down Expand Up @@ -57,7 +56,7 @@ function StringComponent(props: Props) {
}),
[tr, props.errorMessages]
)
const schema = useMemo<JSONSchema7>(
const schema = useMemo<JSONSchema>(
() =>
props.schema ?? {
type: 'string',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import React from 'react'
import { act, renderHook, waitFor } from '@testing-library/react'
import useDataValue from '../useDataValue'
import { Provider } from '../../DataContext'
import { JSONSchema7 } from 'json-schema'
import { FieldBlock, FormError } from '../../Forms'
import { FieldBlock, FormError, JSONSchema } from '../../Forms'

describe('useDataValue', () => {
it('should call external onChange based change callbacks', () => {
Expand Down Expand Up @@ -171,7 +170,7 @@ describe('useDataValue', () => {
})

it('should validate schema', async () => {
const schema: JSONSchema7 = {
const schema: JSONSchema = {
type: 'object',
properties: {
txt: {
Expand Down Expand Up @@ -214,7 +213,7 @@ describe('useDataValue', () => {
})

it('should have correct validation order', async () => {
const schema: JSONSchema7 = {
const schema: JSONSchema = {
type: 'string',
pattern: '^(throw-on-validator)$',
}
Expand Down
8 changes: 5 additions & 3 deletions packages/dnb-eufemia/src/extensions/forms/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { JSONSchema7 } from 'json-schema'
import { SpacingProps } from '../../components/space/types'
import type { SpacingProps } from '../../components/space/types'
import type { JSONSchema7 as JSONSchema } from 'json-schema'

export type { JSONSchema }

type ValidationRule = string | string[]
type MessageValues = Record<string, string>
Expand Down Expand Up @@ -181,7 +183,7 @@ export interface FieldProps<
trim?: boolean
// Validation
required?: boolean
schema?: JSONSchema7
schema?: JSONSchema
validator?: (
value: Value | EmptyValue,
errorMessages?: ErrorMessages
Expand Down

0 comments on commit 96b574c

Please sign in to comment.