### Options Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Demonstrates how to provide an options object to fast-json-stringify. ```javascript const fastJson = require('fast-json-stringify') const stringify = fastJson(mySchema, { schema: { ... }, ajv: { ... }, rounding: 'ceil' }) ``` -------------------------------- ### AnyOf Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md This example shows how to use the `anyOf` keyword in a JSON schema with fast-json-stringify to allow a property to match one of several defined schemas. ```javascript const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { 'undecidedType': { 'anyOf': [{ type: 'string' }, { type: 'boolean' }] } } }) ``` -------------------------------- ### If/Then/Else Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md This example illustrates the support for the `if/then/else` JSON schema feature in fast-json-stringify, showing how conditional logic can be applied to schema validation and serialization. ```javascript const stringify = fastJson({ 'type': 'object', 'properties': { }, 'if': { 'properties': { 'kind': { 'type': 'string', 'enum': ['foobar'] } } }, 'then': { 'properties': { 'kind': { 'type': 'string', 'enum': ['foobar'] }, 'foo': { 'type': 'string' }, 'bar': { 'type': 'number' } } }, 'else': { 'properties': { 'kind': { 'type': 'string', 'enum': ['greeting'] }, 'hi': { 'type': 'string' }, 'hello': { 'type': 'number' } } } }) console.log(stringify({ kind: 'greeting', foo: 'FOO', bar: 42, hi: 'HI', hello: 45 })) // {"kind":"greeting","hi":"HI","hello":45} console.log(stringify({ kind: 'foobar', foo: 'FOO', bar: 42, hi: 'HI', hello: 45 })) // {"kind":"foobar","foo":"FOO","bar":42} ``` -------------------------------- ### Pattern Properties Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Illustrates the usage of 'patternProperties' for dynamic property handling. ```javascript const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { nickname: { type: 'string' } }, patternProperties: { 'num': { type: 'number' }, '.*foo$': { type: 'string' } } }) const obj = { nickname: 'nick', matchfoo: 42, otherfoo: 'str', matchnum: 3 } console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nickname":"nick"}' ``` -------------------------------- ### Additional Properties Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md This example demonstrates how to use the `additionalProperties` keyword in a JSON schema with fast-json-stringify. It shows how properties not explicitly defined in `properties` or `patternProperties` are handled based on the `additionalProperties` setting. ```javascript const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { nickname: { type: 'string' } }, patternProperties: { 'num': { type: 'number' }, '.*foo$': { type: 'string' } }, additionalProperties: { type: 'string' } }) const obj = { nickname: 'nick', matchfoo: 42, otherfoo: 'str', matchnum: 3, nomatchstr: 'valar morghulis', nomatchint: 313 } console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313"}' ``` -------------------------------- ### Unsafe string example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Example of how to use the 'unsafe' format for strings that do not require escaping, offering a performance improvement. ```javascript const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { 'code': { type: 'string', format: 'unsafe' } } }) ``` -------------------------------- ### Defaults Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Shows how to use the 'default' keyword to provide values for undefined or missing fields. ```javascript const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { nickname: { type: 'string', default: 'the default string' } } }) console.log(stringify({})) // '{"nickname":"the default string"}' console.log(stringify({nickname: 'my-nickname'})) // '{"nickname":"my-nickname"}' ``` -------------------------------- ### Example Usage Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Demonstrates how to use fast-json-stringify with a JSON schema to stringify an object. ```javascript const fastJson = require('fast-json-stringify') const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { firstName: { type: 'string' }, lastName: { type: 'string' }, age: { description: 'Age in years', type: 'integer' }, reg: { type: 'string' } } }) console.log(stringify({ firstName: 'Matteo', lastName: 'Collina', age: 32, reg: /"([^\"]|\\\")*"/ })) ``` -------------------------------- ### AnyOf with Required Validation Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md This example demonstrates using `anyOf` with object schemas, emphasizing the importance of the `required` keyword for accurate matching when dealing with objects. ```javascript const stringify = fastJson({ title: 'Example Schema', type: 'array', items: { anyOf: [ { type: 'object', properties: { savedId: { type: 'string' } }, // without "required" validation any object will match required: ['savedId'] }, { type: 'object', properties: { error: { type: 'string' } }, required: ['error'] } ] } }) ``` -------------------------------- ### Standalone Mode Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Shows how to compile code in standalone mode, which can be directly run by Node.js. ```javascript const fs = require('fs') const code = fastJson({ title: 'default string', type: 'object', properties: { firstName: { type: 'string' } } }, { mode: 'standalone' }) fs.writeFileSync('stringify.js', code) const stringify = require('stringify.js') console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"Foo"}' ``` -------------------------------- ### Missing Fields Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Demonstrates how missing fields (not explicitly required) are handled. ```javascript const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { nickname: { type: 'string' }, mail: { type: 'string' } } }) const obj = { mail: 'mail@example.com' } console.log(stringify(obj)) // '{"mail":"mail@example.com"}' ``` -------------------------------- ### Debug Mode Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Demonstrates how to activate and use debug mode to inspect the generated code and restore the stringify function. ```javascript const debugCompiled = fastJson({ title: 'default string', type: 'object', properties: { firstName: { type: 'string' } } }, { mode: 'debug' }) console.log(debugCompiled) // it is a object contain code, ajv instance const rawString = debugCompiled.code // it is the generated code console.log(rawString) const stringify = fastJson.restore(debugCompiled) // use the generated string to get back the `stringify` function console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"Foo"}' ``` -------------------------------- ### Reuse - Inter-external $ref Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Example of external schemas referencing each other. ```javascript const schema = { title: 'Example Schema', type: 'object', properties: { foo: { $ref: 'strings#/definitions/foo' } } } const externalSchema = { strings: { definitions: { foo: { $ref: 'things#/definitions/foo' } } }, things: { definitions: { foo: { type: 'string' } } } } const stringify = fastJson(schema, { schema: externalSchema }) ``` -------------------------------- ### Nullable object example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Demonstrates how to handle nullable objects and properties according to OpenAPI 3.0 specification. ```javascript const stringify = fastJson({ 'title': 'Nullable schema', 'type': 'object', 'nullable': true, 'properties': { 'product': { 'nullable': true, 'type': 'object', 'properties': { 'name': { 'type': 'string' } } } } }) console.log(stringify({product: {name: "hello"}})) // "{"product":{"name":"hello"}}" console.log(stringify({product: null})) // "{"product":null}" console.log(stringify(null)) // null ``` -------------------------------- ### Required Fields Example Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Illustrates how to define required fields in a JSON schema. ```javascript const schema = { title: 'Example Schema with required field', type: 'object', properties: { nickname: { type: 'string' }, mail: { type: 'string' } }, required: ['mail'] } ``` -------------------------------- ### Reuse - $ref Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Example of using $ref to reuse definitions within the same schema. ```javascript const schema = { title: 'Example Schema', definitions: { num: { type: 'object', properties: { int: { type: 'integer' } } }, str: { type: 'string' } }, type: 'object', properties: { nickname: { $ref: '#/definitions/str' } }, patternProperties: { 'num': { $ref: '#/definitions/num' } }, additionalProperties: { $ref: '#/definitions/def' } } const stringify = fastJson(schema) ``` -------------------------------- ### Date Object Serialization Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Example showing how a Date object is serialized using the 'date-time' format. ```javascript const stringify = fastJson({ title: 'Example Schema with string date-time field', type: 'string', format: 'date-time' }) const date = new Date() console.log(stringify(date)) // '"YYYY-MM-DDTHH:mm:ss.sssZ"' ``` -------------------------------- ### Reuse - External $ref Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Example of using $ref to reference external schemas. ```javascript const schema = { title: 'Example Schema', type: 'object', properties: { nickname: { $ref: 'strings#/definitions/str' } }, patternProperties: { 'num': { $ref: 'numbers#/definitions/num' } }, additionalProperties: { $ref: 'strings#/definitions/def' } } const externalSchema = { numbers: { definitions: { num: { type: 'object', properties: { int: { type: 'integer' } } } } }, strings: require('./string-def.json') } const stringify = fastJson(schema, { schema: externalSchema }) ``` -------------------------------- ### Integer rounding customization Source: https://github.com/fastify/fast-json-stringify/blob/main/README.md Customizing integer rounding behavior with the 'rounding' option. ```javascript const stringify = fastJson(schema, { rounding: 'ceil' }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.