fast-json-stringify 5.0.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -102,10 +102,10 @@ function build (schema, options) {
102
102
  ajvInstance.addSchema(schema, rootSchemaId)
103
103
 
104
104
  if (options.schema) {
105
- options.schema = clone(options.schema)
105
+ const externalSchemas = clone(options.schema)
106
106
 
107
- for (const key of Object.keys(options.schema)) {
108
- const externalSchema = options.schema[key]
107
+ for (const key of Object.keys(externalSchemas)) {
108
+ const externalSchema = externalSchemas[key]
109
109
  isValidSchema(externalSchema, key)
110
110
  extendDateTimeType(externalSchema)
111
111
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "5.0.0",
3
+ "version": "5.0.1",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/test/ref.test.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict'
2
2
 
3
+ const clone = require('rfdc')({ proto: true })
4
+
3
5
  const test = require('tap').test
4
6
  const build = require('..')
5
7
 
@@ -1822,3 +1824,70 @@ test('ref with same id in properties', (t) => {
1822
1824
  t.equal(output, '{"_id":"foo","image":{"_id":"bar","name":"hello","owner":"baz"}}')
1823
1825
  })
1824
1826
  })
1827
+
1828
+ test('Should not modify external schemas', (t) => {
1829
+ t.plan(2)
1830
+
1831
+ const externalSchema = {
1832
+ uuid: {
1833
+ format: 'uuid',
1834
+ $id: 'UUID',
1835
+ type: 'string'
1836
+ },
1837
+ Entity: {
1838
+ $id: 'Entity',
1839
+ type: 'object',
1840
+ properties: {
1841
+ id: { $ref: 'UUID' },
1842
+ id2: { $ref: 'UUID' }
1843
+ }
1844
+ }
1845
+ }
1846
+
1847
+ const options = { schema: externalSchema }
1848
+ const optionsClone = clone(options)
1849
+
1850
+ const stringify = build({ $ref: 'Entity' }, options)
1851
+
1852
+ const data = { id: 'a4e4c954-9f5f-443a-aa65-74d95732249a' }
1853
+ const output = stringify(data)
1854
+
1855
+ t.equal(output, JSON.stringify(data))
1856
+ t.same(options, optionsClone)
1857
+ })
1858
+
1859
+ test('input schema is not mutated', (t) => {
1860
+ t.plan(3)
1861
+
1862
+ const schema = {
1863
+ title: 'object with $ref',
1864
+ type: 'object',
1865
+ definitions: {
1866
+ def: { type: 'string' }
1867
+ },
1868
+ properties: {
1869
+ obj: {
1870
+ $ref: '#/definitions/def'
1871
+ }
1872
+ }
1873
+ }
1874
+
1875
+ const clonedSchema = JSON.parse(JSON.stringify(schema))
1876
+
1877
+ const object = {
1878
+ obj: 'test'
1879
+ }
1880
+
1881
+ const stringify = build(schema)
1882
+ const output = stringify(object)
1883
+
1884
+ try {
1885
+ JSON.parse(output)
1886
+ t.pass()
1887
+ } catch (e) {
1888
+ t.fail()
1889
+ }
1890
+
1891
+ t.equal(output, '{"obj":"test"}')
1892
+ t.same(schema, clonedSchema)
1893
+ })