def fields2jsonschema(fields, schema_cls=None, spec=None, use_refs=True, dump=True): """Return the JSON Schema Object for a given marshmallow :class:`Schema <marshmallow.Schema>`. Schema may optionally provide the ``title`` and ``description`` class Meta options. https://github.com/wordnik/swagger-spec/blob/master/versions/2.0.md#schemaObject Example: :: class UserSchema(Schema): _id = fields.Int() email = fields.Email(description='email address of the user') name = fields.Str() class Meta: title = 'User' description = 'A registered user' schema2jsonschema(UserSchema) # { # 'title': 'User', 'description': 'A registered user', # 'properties': { # 'name': {'required': False, # 'description': '', # 'type': 'string'}, # '_id': {'format': 'int32', # 'required': False, # 'description': '', # 'type': 'integer'}, # 'email': {'format': 'email', # 'required': False, # 'description': 'email address of the user', # 'type': 'string'} # } # } :param type schema_cls: A marshmallow :class:`Schema <marshmallow.Schema>` :rtype: dict, a JSON Schema Object """ Meta = getattr(schema_cls, 'Meta', None) if getattr(Meta, 'fields', None) or getattr(Meta, 'additional', None): warnings.warn('Only explicitly-declared fields will be included in the Schema Object. ' 'Fields defined in Meta.fields or Meta.additional are excluded.') ret = {'properties': LazyDict({})} exclude = set(getattr(Meta, 'exclude', [])) for field_name, field_obj in iteritems(fields): if field_name in exclude or (field_obj.dump_only and not dump): continue observed_field_name = _observed_name(field_obj, field_name) prop_func = lambda field_obj=field_obj:\ field2property(field_obj, spec=spec, use_refs=use_refs, dump=dump) # flake8: noqa ret['properties'][observed_field_name] = prop_func if field_obj.required: ret.setdefault('required', []).append(observed_field_name) if Meta is not None: if hasattr(Meta, 'title'): ret['title'] = Meta.title if hasattr(Meta, 'description'): ret['description'] = Meta.description return ret
def fields2jsonschema(fields, schema=None, spec=None, use_refs=True, dump=True, name=None): """Return the JSON Schema Object for a given marshmallow :class:`Schema <marshmallow.Schema>`. Schema may optionally provide the ``title`` and ``description`` class Meta options. https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject Example: :: class UserSchema(Schema): _id = fields.Int() email = fields.Email(description='email address of the user') name = fields.Str() class Meta: title = 'User' description = 'A registered user' schema2jsonschema(UserSchema) # { # 'title': 'User', 'description': 'A registered user', # 'properties': { # 'name': {'required': False, # 'description': '', # 'type': 'string'}, # '_id': {'format': 'int32', # 'required': False, # 'description': '', # 'type': 'integer'}, # 'email': {'format': 'email', # 'required': False, # 'description': 'email address of the user', # 'type': 'string'} # } # } :param Schema schema: A marshmallow Schema instance or a class object :rtype: dict, a JSON Schema Object """ Meta = getattr(schema, 'Meta', None) if getattr(Meta, 'fields', None) or getattr(Meta, 'additional', None): declared_fields = set(schema._declared_fields.keys()) if (set(getattr(Meta, 'fields', set())) > declared_fields or set(getattr(Meta, 'additional', set())) > declared_fields): warnings.warn( "Only explicitly-declared fields will be included in the Schema Object. " "Fields defined in Meta.fields or Meta.additional are ignored." ) jsonschema = { 'type': 'object', 'properties': OrderedLazyDict() if getattr(Meta, 'ordered', None) else LazyDict(), } exclude = set(getattr(Meta, 'exclude', [])) for field_name, field_obj in iteritems(fields): if field_name in exclude or (field_obj.dump_only and not dump): continue observed_field_name = _observed_name(field_obj, field_name) prop_func = lambda field_obj=field_obj: \ field2property(field_obj, spec=spec, use_refs=use_refs, dump=dump, name=name) # flake8: noqa jsonschema['properties'][observed_field_name] = prop_func partial = getattr(schema, 'partial', None) if field_obj.required: if not partial or (is_collection(partial) and field_name not in partial): jsonschema.setdefault('required', []).append(observed_field_name) if 'required' in jsonschema: jsonschema['required'].sort() if Meta is not None: if hasattr(Meta, 'title'): jsonschema['title'] = Meta.title if hasattr(Meta, 'description'): jsonschema['description'] = Meta.description if getattr(schema, 'many', False): jsonschema = { 'type': 'array', 'items': jsonschema, } return jsonschema