Skip to content

Commit

Permalink
fix(json-schema): unset required pros to array type (#3939)
Browse files Browse the repository at this point in the history
fix #3914
  • Loading branch information
aitboudad committed Jun 9, 2024
1 parent bdd1299 commit 32d9fb3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/core/json-schema/src/formly-json-schema.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,8 @@ describe('Service: FormlyJsonschema', () => {
const expectedConfig: FormlyFieldConfig = {
type: 'array',
defaultValue: undefined,
props: { ...emmptyFieldProps, required: true },
templateOptions: { ...emmptyFieldProps, required: true },
props: { ...emmptyFieldProps },
templateOptions: { ...emmptyFieldProps },
fieldArray: expect.any(Function),
validators: expectTypeValidator(['array']),
};
Expand Down Expand Up @@ -2023,6 +2023,25 @@ describe('Service: FormlyJsonschema', () => {
field.props.required = true;
expect(parser('', field)).toEqual('');
});
it('should set non required string to undefined when is empty', () => {
const { field } = renderComponent({
schema: { type: 'string' },
});

const parser = field.parsers[0] as any;

expect(parser('', field)).toEqual(undefined);
field.props.required = true;
expect(parser('', field)).toEqual('');
});

it('should set required string when minLength is set', () => {
const { field } = renderComponent({
schema: { type: 'string', minLength: 1 },
});

expect(field.props.required).toBeTrue();
});
});
});

Expand Down
6 changes: 4 additions & 2 deletions src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ export class FormlyJsonschema {
break;
}
case 'string': {
if (!field.props.required && schema.minLength === 1) {

This comment has been minimized.

Copy link
@jma

jma Jun 10, 2024

To be honest, I do not understand why the required property is related to the minLength value and especially to 1.

This comment has been minimized.

Copy link
@aitboudad

aitboudad Jun 10, 2024

Author Member

Its an issue with the used Angular validator (minLength) which ignore minLength validation when the value is empty. so we need to force validation of this case

This comment has been minimized.

Copy link
@aitboudad

aitboudad Jun 10, 2024

Author Member

Hmm, I think we should set it to required whenever minLength is present.

field.props.required = true;
}

field.parsers = [
(v, f: FormlyFieldConfig) => {
if (types.indexOf('null') !== -1) {
Expand Down Expand Up @@ -451,8 +455,6 @@ export class FormlyJsonschema {
f.key = null;
}

f.props.required = true;

return f;
}

Expand Down

0 comments on commit 32d9fb3

Please sign in to comment.