Skip to content

Commit

Permalink
feat(json-schema): add const validator (#1825)
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad committed Oct 8, 2019
1 parent 92bb6d7 commit 4607b1c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions demo/src/app/examples/advanced/json-schema/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function exclusiveMaximumValidationMessage(err, field: FormlyFieldConfig)
return `should be < ${field.templateOptions.step}`;
}

export function constValidationMessage(err, field: FormlyFieldConfig) {
return `should be equal to constant "${field.templateOptions.const}"`;
}

@NgModule({
imports: [
CommonModule,
Expand All @@ -68,6 +72,7 @@ export function exclusiveMaximumValidationMessage(err, field: FormlyFieldConfig)
{ name: 'minItems', message: minItemsValidationMessage },
{ name: 'maxItems', message: maxItemsValidationMessage },
{ name: 'uniqueItems', message: 'should NOT have duplicate items' },
{ name: 'const', message: constValidationMessage },
],
types: [
{ name: 'string', extends: 'input' },
Expand Down
32 changes: 23 additions & 9 deletions src/core/json-schema/src/formly-json-schema.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,15 +514,29 @@ describe('Service: FormlyJsonschema', () => {
});
});

// TODO: discuss const support possibly as hidden, already set field
// it('should support cosnt', () => {
// const schema: JSONSchema7 = {
// type: 'string',
// const: 'string Const',
// };

// const config = formlyJsonschema.toFieldConfig(schema);
// });
it('should support const as hidden', () => {
const schema: JSONSchema7 = { const: 'const' };
const { type, defaultValue, validators } = formlyJsonschema.toFieldConfig(schema);
expect(type).toBeUndefined();
expect(defaultValue).toEqual('const');
expect(validators).toBeDefined();
});

it('should support const as type', () => {
const schema: JSONSchema7 = {
type: 'string',
const: 'const',
};
const { type, defaultValue, validators: { const: constValidator } } = formlyJsonschema.toFieldConfig(schema);

expect(type).toEqual('string');
expect(defaultValue).toBeUndefined();

expect(constValidator).toBeDefined();
expect(constValidator(new FormControl(null))).toBeFalsy();
expect(constValidator(new FormControl(4))).toBeFalsy();
expect(constValidator(new FormControl('const'))).toBeTruthy();
});
});

// https://json-schema.org/latest/json-schema-validation.html#rfc.section.9
Expand Down
8 changes: 8 additions & 0 deletions src/core/json-schema/src/formly-json-schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ export class FormlyJsonschema {
}
}

if (schema.hasOwnProperty('const')) {
field.templateOptions.const = schema.const;
this.addValidator(field, 'const', ({ value }) => value === schema.const);
if (!field.type) {
field.defaultValue = schema.const;
}
}

if (this.isEnum(schema)) {
field.templateOptions.multiple = field.type === 'array';
field.type = 'enum';
Expand Down

0 comments on commit 4607b1c

Please sign in to comment.