Skip to content
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

feat(json-schema): adding types #2056

Open
wants to merge 1 commit into
base: v5
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions src/core/json-schema/src/formly-json-schema.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,30 @@ describe('Service: FormlyJsonschema', () => {
expect(config.type).toBe(schema.type);
});

describe('should support string types with format', () => {
it('should support type time', () => {
const schema: JSONSchema7 = {
type: 'string',
format: 'time',
};

const { type } = formlyJsonschema.toFieldConfig(schema);

expect(type).toEqual('time');
});

it('should support type date', () => {
const schema: JSONSchema7 = {
type: 'string',
format: 'date',
};

const { type } = formlyJsonschema.toFieldConfig(schema);

expect(type).toEqual('date');
});
});

describe('should support enum type', () => {
it('should support enum as strig array values', () => {
const schemaStringEnum: JSONSchema7 = {
Expand Down
8 changes: 7 additions & 1 deletion src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,10 @@ export class FormlyJsonschema {
return [deps, schemaDeps];
}

private guessType(schema: JSONSchema7) {
private guessType(schema: JSONSchema7): string {
const type = schema.type as JSONSchema7TypeName;
const format = schema.format;

if (!type && schema.properties) {
return 'object';
}
Expand All @@ -426,6 +428,10 @@ export class FormlyJsonschema {
}
}

if (type === 'string' && format !== undefined) {
return format;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should restrict this to the date & time only and check if the type is defined else we should fallback to the Native type by setting type to the templateOptions:

{
  type: 'string',
  templateOptions: {
    type: 'date', // 'time' | 'datetime',
  }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would changing the implementation to something like this be sufficient?

if (type === 'string' && (format === 'date' || format === 'time')) {
  return format;
}

I don't quite understand your point about the templateOptions. Should they be changed as well?

}

return type;
}

Expand Down