Skip to content

Commit

Permalink
fix(json-schema): unset required pros to array type
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 70cbfcf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 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
8 changes: 5 additions & 3 deletions src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,15 @@ export class FormlyJsonschema {
break;
}
case 'string': {
if (!field.props.required && schema.minLength === 1) {
field.props.required = true;
}

field.parsers = [
(v, f: FormlyFieldConfig) => {
if (types.indexOf('null') !== -1) {
v = isEmpty(v) ? null : v;
} else if (f && !f.props.required) {
} else if (f && !f.props.required && !f.props.min) {
v = v === '' ? undefined : v;
}

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 70cbfcf

Please sign in to comment.