N.B: For Angular6-json-schema-form please check this documentation.
Note: This project is a continuation of dschnelldavis/Angular2-json-schema-form and is not affiliated with any organization.
A JSON Schema Form builder for Angular, similar to, and mostly API compatible with:
- JSON Schema Form's Angular Schema Form for AngularJS (examples)
- Mozilla's React JSON Schema Form for React (examples), and
- Joshfire's JSON Form for jQuery (examples)
From 14.0.0 onward, the @ajsf major matches the Angular major it targets, the same
convention Angular Material uses. For the newest Angular major supported by AJSF, install
the current release:
npm install @ajsf/materialnpm uses the latest tag when no version is specified. If your application uses an older
Angular major, install the matching AJSF major instead:
npm install @ajsf/material@17 # for Angular 17Peer ranges are bounded from 14.0.0 onward, so npm reports a clear resolution error
rather than installing a combination that was never built or tested. This also means a
plain install fails when the application is not on the newest supported Angular major.
0.8.0 and earlier predate this scheme. They declare open peer ranges (>=14.0.0) and
were built against Angular 14. There is no release for Angular 15, which reached end of
life. See the versions on npm
for what is currently available.
@ajsf/material no longer depends on @angular/flex-layout,
which is deprecated and has no Angular 16 release. You can uninstall it unless something
else in your project uses it.
No code change is needed. FlexLayoutRootComponent, FlexLayoutSectionComponent, the
ng-jsf-flex-layout widget and layout options such as fxFlex, fxFlexAlign and
fxLayoutGap all behave as before. The version jump is the Angular-aligned scheme
starting, not a rewrite: 14.0.0 targets the same Angular 14 that 0.8.0 did.
Your existing schemas keep working. AJSF stays backward compatible with the older drafts, and nothing you have today needs changing.
| Draft | Status |
|---|---|
| Draft 1, 2, 3, 4 | Supported, converted to draft 6 internally |
| Draft 6 | Supported directly |
| Draft 7 | Supported, including if, then and else |
| 2019-09, 2020-12 | Not supported yet |
Older drafts pass through convertSchemaToDraft6 before the form is built. Keywords it
does not recognise are carried through untouched rather than dropped, which is why draft 7
schemas validate correctly.
Two limits are worth knowing about before you rely on them.
Draft 7 conditionals validate, but the layout does not follow them. if, then and
else are enforced, so a field that becomes required because of another field's value
really is required and the form will not submit without it. The layout is built once,
though, so that field is not marked as required until you try to submit. readOnly and
writeOnly are accepted and currently have no effect: they are annotations, and neither
the validator nor the widgets act on them.
2020-12 is not supported yet. A schema declaring
"$schema": "https://json-schema.org/draft/2020-12/schema" fails to compile and the form
does not render, so it fails loudly rather than quietly. It needs a newer validator, and
prefixItems replaces the array form of items, which is how AJSF recognises tuples.
The playground includes more than 70 JSON Schemas. You can render each one with Material Design, Bootstrap 3, Bootstrap 4, Bootstrap 5, or plain HTML.
Pick the package for the UI you want. @ajsf/material renders with Angular Material, and there are Bootstrap 3, Bootstrap 4 and Bootstrap 5 packages alongside it.
@ajsf/material renders Angular Material components, so it declares @angular/material and @angular/cdk as peer dependencies. Your app needs both installed, with a theme and animations set up. ng add @angular/material does all three:
ng add @angular/materialThen install AJSF. npm uses the current latest release when no version is specified:
npm install @ajsf/materialOr with Yarn:
yarn add @ajsf/materialFor an older Angular major, install the matching AJSF major as described in Version compatibility.
Then import MaterialDesignFrameworkModule in your main application module like this:
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { MaterialDesignFrameworkModule } from '@ajsf/material';
import { AppComponent } from './app.component';
@NgModule({
declarations: [ AppComponent ],
imports: [
BrowserModule,
BrowserAnimationsModule,
MaterialDesignFrameworkModule
],
providers: [],
bootstrap: [ AppComponent ]
})
export class AppModule { }BrowserAnimationsModule is required, not optional: the framework module pulls in MatSelect, MatDatepicker, MatExpansion, MatTabs, MatStepper and others that use animations, and without it the first form throws Found the synthetic property @transformPanel. Use NoopAnimationsModule instead if you want the components without the motion.
Four framework modules are available. Choose the one that matches the UI you want:
MaterialDesignFrameworkModulefrom@ajsf/materialfor Material DesignBootstrap3FrameworkModulefrom@ajsf/bootstrap3for Bootstrap 3Bootstrap4FrameworkModulefrom@ajsf/bootstrap4for Bootstrap 4Bootstrap5FrameworkModulefrom@ajsf/bootstrap5for Bootstrap 5JsonSchemaFormModulefrom@ajsf/corefor plain HTML (no styling)
It is also possible to load multiple frameworks and switch between them at runtime, like the example playground on GitHub. But most typical sites will just load one framework.
To run the library and the example playground from GitHub, assuming you have git and Node installed, enter the following in your terminal:
git clone https://github.com/hamzahamidi/ajsf.git ajsf
cd ajsf
npm ci
npm startThe repository ships a package-lock.json, so use npm rather than Yarn here: npm ci installs exactly the versions CI builds and tests with. See .nvmrc for the Node version.
This starts the example playground at http://localhost:4200.
The main directories are:
projects/ajsf-core: Angular JSON Schema Form main libraryprojects/ajsf-bootstrap3: Bootstrap 3 frameworkprojects/ajsf-bootstrap4: Bootstrap 4 frameworkprojects/ajsf-bootstrap5: Bootstrap 5 frameworkprojects/ajsf-material: Angular Material frameworkprojects/ajsf-core/src/lib/framework-library: framework libraryprojects/ajsf-core/src/lib/widget-library: widget libraryprojects/ajsf-core/src/lib/shared: utilities and helper functionsdemo: example playground applicationdemo/assets/example-schemas: JSON Schema examples used in the playground
There is no generated API reference yet. The functions under projects/ajsf-core/src/lib/shared carry doc comments describing what they do; the widget and framework libraries mostly do not.
For basic use, after loading JsonSchemaFormModule as described above, to display a form in your Angular component, simply add the following to your component's template:
<json-schema-form
loadExternalAssets="true"
[schema]="yourJsonSchema"
framework="no-framework"
(onSubmit)="yourOnSubmitFn($event)">
</json-schema-form>Here, schema is a valid JSON Schema object and onSubmit calls a function that processes the submitted form data. Sample schemas are available in demo/assets/example-schemas.
framework selects the template set to render with. The default is no-framework. The possible values are:
material-designfor Material Designbootstrap-3for Bootstrap 3bootstrap-4for Bootstrap 4bootstrap-5for Bootstrap 5no-frameworkfor plain HTML
Setting loadExternalAssets="true" loads assets the display framework needs from a CDN. It is useful while trying the library out, but production sites should load those assets themselves. See Loading external assets required by a framework for details.
Note what this does and does not cover. For bootstrap-4 and bootstrap-5 it loads Bootstrap's CSS and JavaScript, so a form is styled straight away. For material-design it loads only the Material Icons and Roboto fonts: an Angular Material theme is not included, so add one to your app as ng add @angular/material offers to do, or the controls render unthemed.
Angular JSON Schema Form can also create a form entirely from a JSON object, with no schema, like so:
<json-schema-form
loadExternalAssets="true"
[(ngModel)]="exampleJsonObject">
</json-schema-form>exampleJsonObject = {
"first_name": "Jane", "last_name": "Doe", "age": 25, "is_company": false,
"address": {
"street_1": "123 Main St.", "street_2": null,
"city": "Las Vegas", "state": "NV", "zip_code": "89123"
},
"phone_numbers": [
{ "number": "702-123-4567", "type": "cell" },
{ "number": "702-987-6543", "type": "work" }
], "notes": ""
};In this mode, Angular JSON Schema Form automatically generates a schema from your data. The generated schema is relatively simple, compared to what you could create on your own. However, as the above example shows, it does detect and enforce string, number, and boolean values (nulls are also assumed to be strings), and automatically allows array elements to be added, removed, and reordered.
After displaying a form in this mode, use the formSchema and formLayout outputs to inspect the generated schema and layout. See Debugging inputs and outputs.
The ngModel input supports Angular's bidirectional data binding, so an onSubmit function is not always necessary.
For more control over your form, you may provide these additional inputs:
layoutarray with a custom form layout (see Angular Schema Form's form definitions for information about how to construct a form layout)dataobject to populate the form with default or previously submitted valuesoptionsobject to set any global options for the formwidgetsobject to add custom widgetslanguagestring to set the error message language (currently supportsde,en,es,fr,it,pt, andzh)frameworkstring or object to set which framework to use
For framework, pass a custom framework object or the name of a loaded framework. The included names are material-design, bootstrap-3, bootstrap-4, bootstrap-5, and no-framework.
If you want more detailed output, you may provide additional functions for onChanges to read the values in real time as the form is being filled out, and you may implement your own custom validation indicators from the boolean isValid or the detailed validationErrors outputs.
Here is an example:
<json-schema-form
[schema]="yourJsonSchema"
[layout]="yourJsonFormLayout"
[(data)]="yourData"
[options]="yourFormOptions"
[widgets]="yourCustomWidgets"
language="fr"
framework="material-design"
loadExternalAssets="true"
(onChanges)="yourOnChangesFn($event)"
(onSubmit)="yourOnSubmitFn($event)"
(isValid)="yourIsValidFn($event)"
(validationErrors)="yourValidationErrorsFn($event)">
</json-schema-form>Note: If you prefer brackets around all your attributes, the following is functionally equivalent:
<json-schema-form
[schema]="yourJsonSchema"
[layout]="yourJsonFormLayout"
[(data)]="yourData"
[options]="yourFormOptions"
[widgets]="yourCustomWidgets"
[language]="'fr'"
[framework]="'material-design'"
[loadExternalAssets]="true"
(onChanges)="yourOnChangesFn($event)"
(onSubmit)="yourOnSubmitFn($event)"
(isValid)="yourIsValidFn($event)"
(validationErrors)="yourValidationErrorsFn($event)">
</json-schema-form>With this syntax, include the nested quotes ("' and '") around language and framework names. Without the inner quotes, Angular reads the values as variables instead of strings. Attributes without brackets are read as strings and do not need inner quotes.
You may also combine all your inputs into one compound object and include it as a form input, like so:
const yourCompoundInputObject = {
schema: { ... }, // REQUIRED
layout: [ ... ], // optional
data: { ... }, // optional
options: { ... }, // optional
widgets: { ... }, // optional
language: '...' , // optional
framework: '...' // (or { ... }) optional
}<json-schema-form
[form]="yourCompoundInputObject"
(onSubmit)="yourOnSubmitFn($event)">
</json-schema-form>You can also mix these two styles depending on your needs. In the example playground, all examples use the combined form input for schema, layout, and data, which enables each example to control those three inputs, but the playground uses separate inputs for language and framework, enabling it to change those settings independent of the example.
Combining inputs is useful when each form stores its data and schema together. Separate inputs are often clearer for one form or several forms with the same structure. A custom layout can still be stored with its schema and passed through the combined input.
If you have used Angular Schema Form for AngularJS, React JSON Schema Form, or JSON Form for jQuery, Angular JSON Schema Form recognizes their input names and custom input objects. It also accepts the truncated draft 3 format supported by JSON Form. See JSON Schema versions for the drafts AJSF supports.
Angular Schema Form (AngularJS) compatibility:
<json-schema-form
[schema]="yourJsonSchema"
[form]="yourAngularSchemaFormLayout"
[(model)]="yourData">
</json-schema-form>React JSON Schema Form compatibility:
<json-schema-form
[schema]="yourJsonSchema"
[UISchema]="yourReactJsonSchemaFormUISchema"
[(formData)]="yourData">
</json-schema-form>JSON Form (jQuery) compatibility:
<json-schema-form
[form]="{
schema: yourJsonSchema,
form: yourJsonFormLayout,
customFormItems: yourJsonFormCustomFormItems,
value: yourData
}">
</json-schema-form>Bidirectional data binding works with the data, model, ngModel, and formData inputs. It does not work with the combined form input.
Finally, Angular JSON Schema Form includes some additional inputs and outputs for debugging:
debuginput: activates debugging mode.loadExternalAssetsinput: loads the external JavaScript and CSS the selected framework needs from a CDN. Useful while trying the library out, but not reliable enough for production, where you should load those assets yourself. If the console reports that an asset failed to load (jQuery, for example, which Bootstrap 3 needs), reloading the page usually fixes it.formSchemaandformLayoutoutputs: return the final schema and layout used to build the form. That shows how your inputs were modified, or, if you gave none, the ones generated from your data.
<json-schema-form
[schema]="yourJsonSchema"
[debug]="true"
loadExternalAssets="true"
(formSchema)="showFormSchemaFn($event)"
(formLayout)="showFormLayoutFn($event)">
</json-schema-form>In addition to a large number of user-settable options, Angular JSON Schema Form also has the ability to load custom form control widgets and layout frameworks. All forms are constructed from these basic components. The default widget library includes all standard HTML 5 form controls, as well as several common layout patterns, such as multiple checkboxes and tab sets. The default framework library includes templates to style forms using Material Design, Bootstrap 3, Bootstrap 4, or Bootstrap 5 (or plain HTML with no formatting, which is not useful in production, but can be helpful for development and debugging).
(TODO: List all available user settings, and configuration options for each.)
You can easily add your own custom input validation error messages, either for individual control widgets, or for your entire form.
To set messages for individual form controls, add them to that control's node in the form layout, like this:
const yourFormLayout = [
{ key: 'name',
title: 'Enter your name',
validationMessages: {
// Put your error messages for the 'name' field here
}
},
{ type: 'submit', title: 'Submit' }
]To set messages for the entire form, add them to defautWidgetOptions.validationMessages in the form options. The defautWidgetOptions spelling is part of the public API and is preserved for compatibility.
const yourFormOptions = {
defautWidgetOptions: {
validationMessages: {
// Put your error messages for the entire form here
}
}
}The validationMessages object uses validator names as keys and messages as values. Messages may use any of these formats:
- String: A plain text message, which is always the same.
- String template: Text containing Angular template style
{{variables}}, replaced with values from the returned error object. - Function: A JavaScript function which accepts the error object as input, and returns a string error message.
Here are examples of all three error message types:
validationMessages: {
// String error message
required: 'This field is required.',
// String template error message
// - minimumLength variable will be replaced
minLength: 'Must be at least {{minimumLength}} characters long.',
// Function error message
// Example error: { minimumValue: 2, currentValue: 1 }
minimum: function(error) {
return `Must be at least ${error.minimumValue}; received ${error.currentValue}.`;
}
}The message key must match the validator error it handles. The table below lists the values available to each message.
Here is a list of all the built-in JSON Schema errors, which data type each error is available for, and the values in their returned error objects:
| Error name | Data type | Returned error object values |
|---|---|---|
| required | any | (none) |
| type | any | requiredType, currentValue |
| const | any | requiredValue, currentValue |
| enum | any | allowedValues, currentValue |
| minLength | string | minimumLength, currentLength |
| maxLength | string | maximumLength, currentLength |
| pattern | string | requiredPattern, currentValue |
| format | string | requiredFormat, currentValue |
| minimum | number | minimumValue, currentValue |
| exclusiveMinimum | number | exclusiveMinimumValue, currentValue |
| maximum | number | maximumValue, currentValue |
| exclusiveMaximum | number | exclusiveMaximumValue, currentValue |
| multipleOf | number | multipleOfValue, currentValue |
| minProperties | object | minimumProperties, currentProperties |
| maxProperties | object | maximumProperties, currentProperties |
| dependencies * | object | (varies, based on dependencies schema) |
| minItems | array | minimumItems, currentItems |
| maxItems | array | maximumItems, currentItems |
| uniqueItems | array | duplicateItems |
| contains * | array | requiredItem |
- Note: The
containsanddependenciesvalidators are still in development, and do not yet work correctly.
To add a new widget or override an existing widget, either add an object containing your new widgets to the widgets input of the <json-schema-form> tag, or load the WidgetLibraryService and call registerWidget(widgetType, widgetComponent), with a string type name and an Angular component to be used whenever a form needs that widget type.
Example:
import { YourInputWidgetComponent } from './your-input-widget.component';
import { YourCustomWidgetComponent } from './your-custom-widget.component';
...
const yourNewWidgets = {
'text': YourInputWidgetComponent, // Replace the existing 'text' widget
'custom-control': YourCustomWidgetComponent // Add a new 'custom-control' widget
}Use the widget map in a form:
<json-schema-form
[schema]="yourJsonSchema"
[widgets]="yourNewWidgets">
</json-schema-form>You can also register widgets directly:
import { WidgetLibraryService } from '@ajsf/core';
...
constructor(private widgetLibrary: WidgetLibraryService) { }
...
// Replace the existing 'text' widget:
widgetLibrary.registerWidget('text', YourInputWidgetComponent);
// Add new 'custom-control' widget:
widgetLibrary.registerWidget('custom-control', YourCustomWidgetComponent);Call getAllWidgets() on WidgetLibraryService to inspect the registered widgets. Default widgets are in projects/ajsf-core/src/lib/widget-library, and Material widgets are in projects/ajsf-material/src/lib/widgets. Bootstrap 3, Bootstrap 4 and Bootstrap 5 reformat the default widgets and do not provide custom widgets.
To change the active framework, either use the framework input of the <json-schema-form> tag, or load the FrameworkLibraryService and call setFramework(yourCustomFramework), with either the name of an available framework ('bootstrap-3', 'bootstrap-4', 'bootstrap-5', 'material-design', or 'no-framework'), or with your own custom framework object, like so:
import { YourFrameworkComponent } from './your-framework.component';
import { YourWidgetComponent } from './your-widget.component';
...
const yourCustomFramework = {
framework: YourFrameworkComponent, // required
widgets: { 'your-widget-name': YourWidgetComponent, ... }, // optional
stylesheets: [ '//url-to-framework-external-style-sheet', ... ], // optional
scripts: [ '//url-to-framework-external-script', ... ] // optional
}Use the framework in a form:
<json-schema-form
[schema]="yourJsonSchema"
[framework]="yourCustomFramework">
</json-schema-form>You can also register it directly:
import { FrameworkLibraryService } from '@ajsf/core';
...
constructor(private frameworkLibrary: FrameworkLibraryService) { }
...
frameworkLibrary.setFramework(yourCustomFramework);The required framework key is the Angular component used to format each widget. The optional widgets object overrides or adds widgets. The optional stylesheets and scripts arrays contain external assets loaded when loadExternalAssets is true.
Most UI frameworks need external JavaScript or CSS assets. Load them in your application before rendering an Angular JSON Schema Form. See the setup guides for Bootstrap and Angular Material.
During development, Angular JSON Schema Form can load these resources for you in three ways:
- Call
setFrameworkwith a second parameter oftrue(e.g.setFramework('material-design', true)), or - Add
loadExternalAssets: trueto youroptionsobject, or - Add
loadExternalAssets="true"to your<json-schema-form>tag, as shown above
Finally, if you want to see what scripts a particular framework will automatically load, after setting that framework you can call getFrameworkStylesheets() or getFrameworkScripts() from the FrameworkLibraryService to return the built-in arrays of URLs.
In production, load these assets in the application and remove loadExternalAssets to avoid loading them twice.
Support AJSF through GitHub Sponsors. Sponsors are recognized here according to their selected tier.
See the contributing guide for local setup, tests, and pull request guidance.