Skip to content

Packages/utils/schema #2877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
29e4736
Implemented the utils package
heath-freenome Apr 27, 2022
48cf1f1
- More changes
heath-freenome Apr 28, 2022
66803a2
- Added all of the non-validation-based utilities and many of the tests
heath-freenome May 19, 2022
18ec36f
- removed schema related files temporarily
heath-freenome May 19, 2022
8b2a3a1
- revert `package-lock.json` for antd, core and playground
heath-freenome May 20, 2022
8b5220e
- The schema utilities that require validation
heath-freenome May 20, 2022
b0e9010
- More changes
heath-freenome May 20, 2022
150ff09
- completed conversion to Typescript with some changes that were needed
heath-freenome May 24, 2022
c8ad992
- Updated various files to make some parameters optional, to add gene…
heath-freenome May 24, 2022
dca2450
- Implemented createSchemaUtils
heath-freenome May 27, 2022
cbb27aa
- More fixes and adding missing generics
heath-freenome May 31, 2022
39d9d44
- Converted all the schema based tests over still missing 100% coverage
heath-freenome Jun 7, 2022
51dbca6
- Completed 100% unit testing
heath-freenome Jun 7, 2022
1ead960
- Added documentation for most of the files and cleaned up some optio…
heath-freenome Jun 8, 2022
bbb3de2
- Completed all of the documentation of the schema-based utils
heath-freenome Jun 9, 2022
251f850
- Changed the `_NAME` constants to `_KEY`
heath-freenome Jun 9, 2022
7c6a348
- Added missing generics on a few types
heath-freenome Jun 16, 2022
fed0877
- Added `ERRORS_KEY` constant for use in validation
heath-freenome Jun 16, 2022
f3ef93a
- Fixed types based on implementation of validator
heath-freenome Jun 21, 2022
97f40bc
- Added missing generic to the `ValidationData` type for `ErrorSchema`
heath-freenome Jun 22, 2022
55ae809
- Fixed tests for the missing generic type
heath-freenome Jun 23, 2022
5249a66
- Incorporated #2876 by making props for UISchemaSubmitButtonOptions …
heath-freenome Jun 23, 2022
c6217d3
- Added generic `<T>` to the return value of `toIdSchema()` and `toPa…
heath-freenome Jun 24, 2022
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
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"jest-expect-message"
],
"testMatch": [
"**/test/**/*.test.[jt]s?(x)"
"**/test/**/*.test.ts?(x)"
],
"coverageDirectory": "<rootDir>/coverage/",
"collectCoverage": true,
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
export const ADDITIONAL_PROPERTY_FLAG = '__additional_property';
export const ADDITIONAL_PROPERTIES_NAME = 'additionalProperties';
export const ALL_OF_NAME = 'allOf';
export const ANY_OF_NAME = 'anyOf';
export const CONST_NAME = 'const';
export const DEFAULT_NAME = 'default';
export const DEPENDENCIES_NAME = 'dependencies';
export const ITEMS_NAME = 'items';
export const ONE_OF_NAME = 'oneOf';
export const PROPERTIES_NAME = 'properties';
export const SUBMIT_BTN_OPTIONS_NAME = 'submitButtonOptions';
export const REF_NAME = '$ref';
Expand Down
69 changes: 69 additions & 0 deletions packages/utils/src/createSchemaUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { RJSFSchema, SchemaUtilsType, UiSchema, ValidatorType } from './types';
import {
getDefaultFormState,
getDisplayLabel,
getMatchingOption,
isFilesArray,
isMultiSelect,
isSelect,
retrieveSchema,
stubExistingAdditionalProperties,
toIdSchema,
toPathSchema,
} from './schema';

export class SchemaUtils<T = any> implements SchemaUtilsType<T> {
rootSchema: RJSFSchema;
validator: ValidatorType;

constructor(validator: ValidatorType, rootSchema: RJSFSchema) {
this.rootSchema = rootSchema;
this.validator = validator;
}

getDefaultFormState(schema: RJSFSchema, formData?: T, includeUndefinedValues = false): T | T[] | undefined {
return getDefaultFormState<T>(this.validator, schema, formData, this.rootSchema, includeUndefinedValues);
}

getDisplayLabel<F = any>(schema: RJSFSchema, uiSchema: UiSchema<T, F>) {
return getDisplayLabel<T, F>(this.validator, schema, uiSchema, this.rootSchema);
}

getMatchingOption(formData: T, options: RJSFSchema[]) {
return getMatchingOption<T>(this.validator, formData, options, this.rootSchema);
}

isFilesArray<F = any>(schema: RJSFSchema, uiSchema: UiSchema<T, F>) {
return isFilesArray<T, F>(this.validator, schema, uiSchema, this.rootSchema);
}

isMultiSelect(schema: RJSFSchema) {
return isMultiSelect<T>(this.validator, schema, this.rootSchema);
}

isSelect(schema: RJSFSchema) {
return isSelect<T>(this.validator, schema, this.rootSchema);
}

retrieveSchema(schema: RJSFSchema, rawFormData: T) {
return retrieveSchema<T>(this.validator, schema, this.rootSchema, rawFormData);
}

stubExistingAdditionalProperties(schema: RJSFSchema, formData: T) {
return stubExistingAdditionalProperties<T>(this.validator, schema, this.rootSchema, formData);
}

toIdSchema(schema: RJSFSchema, id: string, formData: T, idPrefix = 'root', idSeparator = '_') {
return toIdSchema<T>(this.validator, schema, id, this.rootSchema, formData, idPrefix, idSeparator);
}

toPathSchema(schema: RJSFSchema, name: string, formData: T) {
return toPathSchema<T>(this.validator, schema, name, this.rootSchema, formData);
}
}

export default function createSchemaUtils<T = any>(
validator: ValidatorType, rootSchema: RJSFSchema
): SchemaUtilsType<T> {
return new SchemaUtils<T>(validator, rootSchema);
}
23 changes: 19 additions & 4 deletions packages/utils/src/findSchemaDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@ import jsonpointer from 'jsonpointer';
import omit from 'lodash/omit';

import { REF_NAME } from './constants';
import { RJSFSchema } from './types';
import { GenericObjectType, RJSFSchema } from './types';

/** Splits out the value at the `key` in `object` from the `object`, returning an array that contains in the first
* location, the `object` minus the `key: value` and in the second location the `value`.
*
* @param key - The key from the object to extract
* @param object - The object from which to extract the element
* @returns - An array with the first value being the object minus the `key` element and the second element being the
* value from `object[key]`
*/
export function splitKeyElementFromObject(key: string, object: GenericObjectType) {
const value = object[key];
const remaining = omit(object, [key]);
return [remaining, value];
}

/** Given the name of a `$ref` from within a schema, using the `rootSchema`, look up and return the sub-schema using the
* path provided by that reference. If `#` is not the first character of the reference, or the path does not exist in
Expand All @@ -25,9 +39,10 @@ export default function findSchemaDefinition($ref?: string, rootSchema: RJSFSche
throw new Error(`Could not find a definition for ${$ref}.`);
}
if (current[REF_NAME]) {
const subSchema = findSchemaDefinition(current[REF_NAME]!, rootSchema);
if (Object.keys(current).length > 1) {
return { ...omit(current, [REF_NAME]), ...subSchema };
const [remaining, theRef] = splitKeyElementFromObject(REF_NAME, current);
const subSchema = findSchemaDefinition(theRef, rootSchema);
if (Object.keys(remaining).length > 0) {
return { ...remaining, ...subSchema };
}
return subSchema;
}
Expand Down
83 changes: 4 additions & 79 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import allowAdditionalItems from './allowAdditionalItems';
import asNumber from './asNumber';
import canExpand from './canExpand';
import createSchemaUtils from './createSchemaUtils';
import dataURItoBlob from './dataURItoBlob';
import deepEquals from './deepEquals';
import findSchemaDefinition from './findSchemaDefinition';
Expand Down Expand Up @@ -29,92 +30,16 @@ import toConstant from './toConstant';
import toDateString from './toDateString';
import utcToLocal from './utcToLocal';

import {
ArrayFieldTemplateItemType,
ArrayFieldTemplateProps,
CustomValidator,
DateObject,
DescriptionFieldProps,
ErrorListProps,
ErrorSchema,
ErrorTransformer,
Field,
FieldError,
FieldErrors,
FieldId,
FieldPath,
FieldProps,
FieldTemplateProps,
FieldValidation,
FormValidation,
GenericObjectType,
IChangeEvent,
IdSchema,
ObjectFieldTemplatePropertyType,
ObjectFieldTemplateProps,
PathSchema,
RangeSpecType,
Registry,
RegistryFieldsType,
RegistryWidgetsType,
RJSFSchema,
RJSFValidationError,
TitleFieldProps,
UiSchema,
UIOptionsType,
UISchemaSubmitButtonOptions,
ValidationData,
ValidatorType,
Widget,
WidgetProps,
} from './types';

export type {
ArrayFieldTemplateItemType,
ArrayFieldTemplateProps,
CustomValidator,
DateObject,
DescriptionFieldProps,
ErrorListProps,
ErrorSchema,
ErrorTransformer,
Field,
FieldError,
FieldErrors,
FieldId,
FieldPath,
FieldProps,
FieldTemplateProps,
FieldValidation,
FormValidation,
GenericObjectType,
IChangeEvent,
IdSchema,
ObjectFieldTemplatePropertyType,
ObjectFieldTemplateProps,
PathSchema,
RangeSpecType,
Registry,
RegistryFieldsType,
RegistryWidgetsType,
RJSFSchema,
RJSFValidationError,
TitleFieldProps,
UiSchema,
UIOptionsType,
UISchemaSubmitButtonOptions,
ValidationData,
ValidatorType,
Widget,
WidgetProps,
};
export * from './types';

export * from './constants';
export * from './schema';

export {
allowAdditionalItems,
asNumber,
canExpand,
createSchemaUtils,
dataURItoBlob,
deepEquals,
findSchemaDefinition,
Expand Down
Loading